This question already has answers here:
How to read a specific line using the specific line number from a file in Java?
(19 answers)
Closed 5 years ago.
My program is reading all lines in the file but i just need the second one.
String line;
try (
InputStream fis = new FileInputStream(source);
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr)) {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
If you only need the second line and you are sure the file always has at least two lines, you can just read twice and ignore the first time.
br.readLine(); //read, but ignore
System.out.println(br.readLine()); // read and output
Related
This question already has answers here:
Find a line in a file and remove it
(17 answers)
Closed 5 years ago.
I need to remove lines from txt file a
FileReader fr= new FileReader("Name3.txt");
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
br.close();
and I don't know the continue of the code.
You can read all lines and store them in a list. Whilst you're storing all lines, assuming you know the line you want to remove, simply check for the lines you don't want to store, and skip them. Then write the list content to a file.
//This is the file you are reading from/writing to
File file = new File("file.txt");
//Creates a reader for the file
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
//This is your buffer, where you are writing all your lines to
List<String> fileContents = new ArrayList<String>();
//loop through each line
while ((line = br.readLine()) != null) {
//if the line we're on contains the text we don't want to add, skip it
if (line.contains("TEXT_TO_IGNORE")) {
//skip
continue;
}
//if we get here, we assume that we want the text, so add it
fileContents.add(line);
}
//close our reader so we can re-use the file
br.close();
//create a writer
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
//loop through our buffer
for (String s : fileContents) {
//write the line to our file
bw.write(s);
bw.newLine();
}
//close the writer
bw.close();
This question already has answers here:
Reading a plain text file in Java
(31 answers)
Closed 5 years ago.
I have text file which i have read once user upload, how can i read using java code, can any one give some suggestion which would very helpful to me
sample file looks like below
SNR Name
1 AAR
2 BAT
3 VWE
A common pattern is to use
try (BufferedReader br = new BufferedReader(new FileReader(file)) ) {
String line;
while ((line = br.readLine()) != null) {
// process the line to insert in database.
}}
The easiest way is use Scanner() object
Scanner sc = new Scanner(new File("myFile.txt"));
use
boolean hasNext = sc.hasNext();
to know if there are more items in the file
and
String item = sc.next();
to get items secuentially.
I attach the documentation (it provides very good code examples)
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
You can use BufferedReader to read file line by line.
So, even if your file is too big, then also it will read line by line only. It won't load entire file. Declaration will be similar to this.
BufferedReader br = new BufferedReader(new FileReader(FILENAME));
And
you can use command
while ((sCurrentLine = br.readLine()) != null) { .....// process
}
to read file till end.
Obviously, you have to use seperator in file to split the records in each line. And store accordingly in java objects.
After that you can store in DB through DAO.
This question already has answers here:
Using BufferedReader.readLine() in a while loop properly
(7 answers)
Closed 7 years ago.
if you have something like this
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
Why does bufferedeader.readline() read the next line after the first one? What's confusing to me is that there isn't a readnextline method and I don't understand why readline would continue reading the rest of the file instead of looping the first line infinitely.
You can rewrite this to:
line = bufferedReader.readLine()
while (line != null) {
... print ...
line = bufferedReader.readLine();
That should answer you question ...
(the point being the fact that readLine(); well, reads ONE line; after the other; and returns null if there wasnt any more line to read)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create a Java String from the contents of a file
I have a .txt file that I want to save in a String variable. I imported the file with File f = new File("test.txt");. Now I am trying to put the contents of it in a String variable. I can not find a clear explanation for how to do this.
Use a Scanner:
Scanner file = new Scanner(new File("test.txt"));
String contents = file.nextLine();
file.close();
Of course, if your file has multiple lines you can call nextLine multiple times.
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Read/write .txt file with special characters
Im reading a file but I dont know how to read the accents and special caracters, here is mo code for read I read I have to add a different codification but i dont know how to doit
File file = new File("C://fichero.csv");
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
line = bufRdr.readLine();
Thanks
Try with the following:
File file = new File("C://fichero.csv");
BufferedReader bufRdr = new BufferedReader(
new InputStreamReader(new FileInputStream(file),"ISO-8859-1"));
String line = null;
line = bufRdr.readLine();