Java Read Files
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
public class ReadFile {
public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
}
Also in Java:
- Java use Base64
- how to check the lines in a file java scanner
- java not instanceof
- java remove double spaces
- how to read to into a file in java
- kotlin android intent pass data
- java math.random
- convert char to string java
- append to arraylist by index java
- floor in java
- android how to split string
- initialize scanner java
- remove duplicates from singly linked list
- java string split from input string
- java.lang.UnsupportedOperationException
- java hashmap put
- converter int array para string java
- java min function
- index of an array procesing
- org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
- how to quicksort a string array in java
- action on long press of edit edittext
- java singleton implementation
- java script to detect the crome browser