I am a newbie to java. I am reading a big file using BufferedReader. How can I know the offset of a line so that I can store it in some other file for indexing purposes?
EDIT:
Here's my code
path=FileSystems.getDefault().getPath(".",filename);
br=Files.newBufferedReader(path_doc_title_index_path, Charset.defaultCharset());
int offset=0; //offset of first line.
String strline=br.readline();
offset+=strline.length()+1; //offset of second line
If you are asking which line you are reading in a file, a simple line counter would work
BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt"));
int lineNum = 0;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
lineNum++;
}
Related
I am trying to read parts of a text file with the format
John Smith
72
160
The first line being the name (string), and the second and third lines being height and weight (both ints). However, I cannot find a way to store each of these into their own variables, instead I can only figure out how to store the whole thing into one variable and print it. This is the code that I have as of now
try
{
File file = new File("person.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null)
{
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println(stringBuffer);
}
In this part
stringBuffer.append(line);
stringBuffer.append("\n");
I was thinking of trying to add a part in the middle of both those lines that stored a variable, but it did not seem possible. I also thought of using a for loop and using that to my advantage somehow, but could not figure out a way to do it with that either.
Is there any possible way to do this that I do not know about? Thank you
Reading and parsing a text file in Java has been getting easier in every new version. You can try the following way:
List<String> lines = Files.lines(Paths.get("person.txt")).collect(Collectors.toList());
String name = lines.get(0);
Integer height = Integer.parseInt(lines.get(1));
Integer weight = Integer.parseInt(lines.get(2));
File file = new File("person.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String firstline = bufferedReader.readLine();
String secondline = bufferedReader.readLine();
String thirdline = bufferedReader.readLine();
fileReader.close();
i have to count the lines on a file but later in the code I also have to print what's in that file, but I can't use the reader twice it just says null. How can I work this out without creating a bunch of bufferedreader objects?
thanks
Print and count at the same time?
Move the lines to an array then print them?
Make sure you've closed the file before reopening again?
Try closing the buffer, and then re-opening it again.
BufferedReader bufferedReader = new BufferedReader(new FileReader("src/main/java/com/william/sandbox/stackoverflow/samples20160306/Demo.java"));
String line = bufferedReader.readLine();
int lineCount = 0;
while(line != null){
lineCount += 1;
line = bufferedReader.readLine();
}
System.out.println("Line count is: " + lineCount);
bufferedReader.close();
bufferedReader = new BufferedReader(new FileReader("src/main/java/com/william/sandbox/stackoverflow/samples20160306/Demo.java"));
line = bufferedReader.readLine();
while(line != null){
System.out.println(line);
line = bufferedReader.readLine();
}
}
You can use BufferedReader's mark() and reset() methods to jump back to a specific position.
try (BufferedReader r = new BufferedReader(new FileReader("somefile.txt"))) {
// marks this position for the next 10 characters read
// after that the mark is lost
r.mark(10);
// do some reading
// jump back to the mark
r.reset();
}
Note that, BufferedReader supports marking but not all Readers do. You can use markSupported() to check.
I have a text file. I want to read it line by line and turn it into an 2-dimensional array. I have written something as follows:
BufferedReader br = new BufferedReader (new FileReader ("num.txt"));
String line = br.readLine();
while( line != null) {
System.out.printf(line);
}
This turns into an infinite loop. I want to move on to the next line after I'm done with reading and printing a line. But I don't know how to do that.
You only read the first line. The line variable didn't change in the while loop, leading to the infinite loop.
Read the next line in the while condition, so each iteration reads a line, changing the variable.
BufferedReader br = new BufferedReader (new FileReader ("num.txt"));
String line;
while( (line = br.readLine() ) != null) {
System.out.printf(line);
}
BufferedReader br = new BufferedReader (new FileReader ("num.txt"));
String line = br.readLine();
while( line != null) {
System.out.printf(line);
// read the next line
line = br.readLine();
}
... or read the line in the while condition (as rgettman pointed out):
String line;
while( (line = br.readLine()) != null) {
System.out.printf(line);
}
Every time I do something like
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
It won't outprint line by line. Instead it will lag for 2-3 seconds then show it all at once. I've tried putting sleep methods etc. How can I make it so it takes its time and goes through each one rather than just lagging and spitting it all out at once?
Try flushing the output.
System.out.flush();
After each System.out.println
This is probably because your file doesn't have carriage return characters at the end of each line. So, it's considering the entire file as one line.
Try sending the lines to an array list. See if the arraylist contains each line. Then try iterating the array list to the website chunk by chunk.
BufferedReader br = new BufferedReader(new FileReader(f));
ArrayList<String> list = new ArrayList<String>();
String line;
while ((line = br.readLine()) != null) {
list.add(line);
}
br.close();
for(String one_line: list){
//send lines to website:
//sendLine(one_line);
}
I am trying to read some lines from a file in Java. I have 4 lines in the file, but the problem is that it reads only 2 of the lines. Here's the code:
BufferedReader flux_in = new BufferedReader(new InputStreamReader(new FileInputStream("abc.txt")));
String line;
while (flux_in.readLine() != null)
{
line = flux_in.readLine();
System.out.println(line);
}
It is because you are calling readLine twice as often as you should.
Your first call inside the the while condition just threw the line away.
BufferedReader flux_in = new BufferedReader(new InputStreamReader(new FileInputStream("abc.txt")));
String line;
while ((line = flux_in.readLine()) != null)
{
System.out.println(line);
}
It does read all of them, although not quite in the way you'd like.
BufferedReader flux_in = new BufferedReader(new InputStreamReader(new FileInputStream("abc.txt")));
String line;
while (flux_in.readLine()!=null) //one line is read here
{
line = flux_in.readLine(); //the next one here
System.out.println(line);
}
In your code, the call in the loop test consumes a line, and then the call n the loop body consumes another line. So it would only print every other line.
String s = null;
while ((s = flux_in.readLine()) != null)
{
System.out.println(s);
}
BufferedReader is stateful and remembers what has been read from the file already. Every call of readLine() moves the cursor to the next line. You are calling readLine() twice per line: in the while loop and in the line assignment. Try this instead:
String line = flux_in.readLine();
while (line != null) {
System.out.println(line);
line = flux_in.readLine();
}
You're reading the line twice (in the while condition and in the while loop, so it's displaying one and then skipping the next. Change your code to this:
try {
BufferedReader flux_in = new BufferedReader(new FileReader("abc.txt"));
String line;
while ((line = flux_in.readLine()) != null)
System.out.println(line);
} catch(Exception e) { System.out.println("Error"); }