BufferedReader always resets to last character in a file - java

I am having trouble reseting BufferedReader to a random point in reading a txt file. My example input text in a file is: number = 10;.
Last part of code System.out.println((char)c); will print out ; instead of 1.
What am I doing wrong in this simple example?
My code:
String filePath = "data.txt";
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line = "";
int c = 0;
while ((c = br.read()) != -1) {
if((char)c == '1') {
br.mark(1000);
}
System.out.print((char)c);
}
br.reset();
c = br.read();
System.out.println((char)c);

From using your code and some dummy .txt file I made using the number=10; as a line. c = br.read() returns 0.
The java.io.BufferedReader.mark(int) method marks the current position in the stream. Invoking reset() will reposition the stream to this point, from here.
So essentially it prints the character after the character you have assigned as the mark.
If you wish to print the rest of the line you could use:
String line = br.readLine();
If you do want to get the 1 for some reason, you could simply modify your code as such:
c = br.read()+1;

I tried your code, first of all it is printing '0' in my case.
Actually when you use reset its putting cursor after 1 not before 1.
But according to your need you need output as 1, which can be possible if you use mark on the character before 1.
Like I wanted to print r so I put mark on e as shown below:
while ((c = br.read()) != -1) {
if((char)c == 'e') {
br.mark(1000);
}
System.out.print((char)c);
}
br.reset();
c = br.read();
System.out.println((char)c);
Hope it makes thing clear.

Related

Adding number of lines with BufferedWriter/Reader

Please help me with adding number of lines of original file.
Im trying to write lines which met specified conditions from file A to file B. There is no problem, but I need to write number of lines of file A to the end of file B and also add number of lines of file B.
Program runs through file A, if condition is met, line is written to the file B, however I stuck to add number of lines after that.
For the last 2 hours I already tried second BufferedWriter, second "try" block, bw.write(lines) and many more, but nothing worked. System.out.println(lines) worked good, so Im really confused.
Here's my actual code:
try (
BufferedReader bReader = new BufferedReader(new FileReader("fileA.txt"));
BufferedWriter bWriter = new BufferedWriter(new FileWriter("fileB.txt"));
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader("fileA.txt"));
){
lineNumberReader.skip(Long.MAX_VALUE);
int lines = lineNumberReader.getLineNumber();
lineNumberReader.close();
String line;
while ((line = bReader.readLine()) != null){
String[] values = line.split(" ");
int time = Integer.parseInt(values[3]);
if (time < 16){
bWriter.write(values[0] + ' ' + values[1] + ' ' + values[2] + "\n");
}
}
}
You can do that by adding bWriter.write(""+lines); after while loop.
You have to use bWriter.write(string) instead of bWriter.write(int).
bWriter.write(string) - the string value is written to the file as is.
bWriter.write(int); - It won't work because the int passed is converted to corresponding char and is then written to the file. The char corresponding to your lines value must be some non-printable character, and hence you are not able to see it.

Buffered Reader find specific line separator char then read that line

My program needs to read from a multi-lined .ini file, I've got it to the point it reads every line that start with a # and prints it. But i only want to to record the value after the = sign. here's what the file should look like:
#music=true
#Volume=100
#Full-Screen=false
#Update=true
this is what i want it to print:
true
100
false
true
this is my code i'm currently using:
#SuppressWarnings("resource")
public void getSettings() {
try {
BufferedReader br = new BufferedReader(new FileReader(new File("FileIO Plug-Ins/Game/game.ini")));
String input = "";
String output = "";
while ((input = br.readLine()) != null) {
String temp = input.trim();
temp = temp.replaceAll("#", "");
temp = temp.replaceAll("[*=]", "");
output += temp + "\n";
}
System.out.println(output);
}catch (IOException ex) {}
}
I'm not sure if replaceAll("[*=]", ""); truly means anything at all or if it's just searching for all for of those chars. Any help is appreciated!
Try following:
if (temp.startsWith("#")){
String[] splitted = temp.split("=");
output += splitted[1] + "\n";
}
Explanation:
To process lines only starting with desired character use String#startsWith method. When you have string to extract values from, String#split will split given text with character you give as method argument. So in your case, text before = character will be in array at position 0, text you want to print will be at position 1.
Also note, that if your file contains many lines starting with #, it should be wise not to concatenate strings together, but use StringBuilder / StringBuffer to add strings together.
Hope it helps.
Better use a StringBuffer instead of using += with a String as shown below. Also, avoid declaring variables inside loop. Please see how I've done it outside the loop. It's the best practice as far as I know.
StringBuffer outputBuffer = new StringBuffer();
String[] fields;
String temp;
while((input = br.readLine()) != null)
{
temp = input.trim();
if(temp.startsWith("#"))
{
fields = temp.split("=");
outputBuffer.append(fields[1] + "\n");
}
}

Reading data from a specific line and counting words from nth line to print nth word on that line

I have this program that reads a text file. I need to get some data out of it.
The text files look like this:
No. Ret.Time Peak Name Height Area Rel.Area Amount Type
min µS µS*min % mG/L
1 2.98 Fluoride 0.161 0.028 0.72 15.370 BMB
2 3.77 Chloride 28.678 3.784 99.28 2348.830 BMB
Total: 28.839 3.812 100.00 2364.201
I need to start reading from line #29 and from there get the Peak Name and the Amount of each element like Fluoride, Chloride and so on. The example only shows those two elements, but other text files will have more. I know I will need some sort of loop to iterate through those lines starting on line #29 which is where the "1" starts then the "2" which will be the 30th line and so on.
I have tried to make this work, but I am missing something I think and I`m not sure what. Here is my Code.
int lines = 0;
BufferedReader br = new BufferedReader(new FileReader(selectFile.getSelectedFile()));
Scanner sc = new Scanner(new FileReader(selectFile.getSelectedFile()));
String word = null;
while((word =br.readLine()) != null){
lines++;
/*if(lines == 29)
System.out.println(word);*/
if ((lines == 29) && sc.hasNext())
count++;
String value = sc.next();
if (count == 2)
System.out.println(value + ",");
}
Here's some code for you:
int linesToSkip = 28;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ( (line = br.readLine()) != null) {
if (linesToSkip-- > 0) {
continue;
}
String[] values = line.split(" +");
int index = 0;
for (String value : values) {
System.out.println("values[" + index + "] = " + value);
index++;
}
}
}
Note that I've surrounded it in a try(expr) {} block to ensure that the reader is closed at the end, otherwise you'll consume resources and possibly lock the file from other processes.
I've also renamed the variable you called word as line to make it clearer what it contains (i.e. a string representing a line in the file).
The line.split(" +") uses a regular expression to split a String into its constituent values. In this case your values have spaces between, so we're using " +" which means 'one or more spaces'. I've just looped through the values and printed them out; obviously, you will need to do whatever it is you need to do with them.
I replaced the line count with a linesToSkip variable that decrements. It's less code and explains better what you're trying to achieve. However, if you need the line number for some reason then use that instead, as follows:
if (++lineCount <= 28) {
continue;
}
If I'm reading it correctly, you are mixing two different readers with the BufferedReader and the Scanner, so you are not going to get the results right changing from one to the other (one is not pointing to the same position than the other). You already have the line in word and you can parse it, no need of using the Scanner. Just skip until line 29 (lines > 29) and then parse the values you want, line by line.
You are reading the file twice... try something like this
int lines = 0;
BufferedReader br = new BufferedReader(new FileReader(selectFile.getSelectedFile()));
String line = null;
while ((line = br.readLine()) != null) {
if (++lines < 29)
continue; //this ignores the line
for(String word : line.split("separator here")) {
// this will iterate over every word on that line
// I think you can take it from here
System.out.println(word);
}
}

Read a text file until EOL in Java

I am trying to read a text file which has -
hello James!
How are you today!
I want to read the each character in the string till i find EOL character.As i am using windows where i have /n/r which represents EOL character.How can i write a condition to go through all the characters of the string and print them one by one till it reaches EOL(/n/r).
int readedValue;
do
{
while((readedValue = bufferReader.read()) != 10)
{
//readedValue = bufferReader.read();
char ch = (char) readedValue;
System.out.print(ch);
}
}
while ((readedValue = bufferReader.read()) != -1);
when i read the file now , i get out put as hello James!ow are you today!
I am not getting 'H'ow in How. What can i alter this to get the complete text?
As people have noted, the readline() method reads to the next line separator, and returns the line with the separator removed. So your tests for '\n' and '\r' in line cannot possibly evaluate to true.
But you can easily add an extra end-of-line when you output the line string1.
1 - that is, unless you actually need to preserve the exact same end-of-line sequence characters as in the input stream.
You ask:
Instead of using readline(), Is there any way i can use buffer reader to read each character and print them?
Yea, sure. The read() method returns either one character or -1 to indicate EOF. So:
int ch = br.read();
while (ch != -1) {
System.out.print((char) ch);
ch = br.read();
}
You can use something like that:
while((line=input.readLine())!=null) {
// do something
}
If you want to read char by char, you can use this:
int readedValue;
while ((readedValue = reader.read()) != -1) {
char ch = (char) readedValue;
// do something
}
Here is an example (with a string instead a file) for your new problem:
String line;
int readedValue;
String s = "hello James!\n\rHow are you today!";
StringReader input = new StringReader(s);
BufferedReader lineReader= new BufferedReader (input);
while((line=lineReader.readLine())!=null) {
StringReader input2 = new StringReader(line);
BufferedReader charReader= new BufferedReader (input2);
while((readedValue = charReader.read()) != -1) {
char ch = (char) readedValue;
System.out.print(ch);
}
}
Your problem is about magic numbers.
your while will enter into an infinite loop in the case charAt(21)!='\n' && charAt(22)!='\r'
those two integers shall be increased inside the loop.
charAt(i)!='\n' && charAt(i+1)!='\r'
::inside loop
i++

Reading Integer values from a file (Java)

I'm working on a simple level editor for my Android game. I've written the GUI (which draws a grid) using swing. You click on the squares where you want to position a tile and it changes colour. Once you're done, you write everything to a file.
My file consists of something like the following (this is just an example):
I use the asterisks to determine the level number being read and the hyphen to tell the reader to stop reading.
My file reading code is below, Selecting which part to read works OK - for example. if I pass in 2 by doing the following:
readFile(2);
Then it prints all of the characters in the 2nd section
What I can't figure out is, once I've got to the 'start' point, how do I actually read the numbers as integers and not individual characters?
Code
public void readFile(int level){
try {
//What ever the file path is.
File levelFile = new File("C:/Temp/levels.txt");
FileInputStream fis = new FileInputStream(levelFile);
InputStreamReader isr = new InputStreamReader(fis);
Reader r = new BufferedReader(isr);
int charTest;
//Position the reader to the relevant level (Levels are separated by asterisks)
for (int x =0;x<level;x++){
//Get to the relevant asterisk
while ((charTest = fis.read()) != 42){
}
}
//Now we are at the correct read position, keep reading until we hit a '-' char
//Which indicates 'end of level information'
while ((charTest = fis.read()) != 45){
System.out.print((char)charTest);
}
//All done - so close the file
r.close();
} catch (IOException e) {
System.err.println("Problem reading the file levels.txt");
}
}
Scanner's a good answer. To remain closer to what you have, use the BufferedReader to read whole lines (instead of reading one character at a time) and Integer.parseInt to convert from String to Integer:
// get to starting position
BufferedReader r = new BufferedReader(isr);
...
String line = null;
while (!(line = reader.readLine()).equals("-"))
{
int number = Integer.parseInt(line);
}
If you use the BufferedReader and not the Reader interface, you can call r.readLine(). Then you can simply use Integer.valueOf(String) or Integer.parseInt(String).
Perhaps you should consider using readLine which gets all the chars up the the end of line.
This part:
for (int x =0;x<level;x++){
//Get to the relevant asterisk
while ((charTest = fis.read()) != 42){
}
}
Can change to this:
for (int x =0;x<level;x++){
//Get to the relevant asterisk
while ((strTest = fis.readLine()) != null) {
if (strTest.startsWith('*')) {
break;
}
}
}
Then, to read the values another loop:
for (;;) {
strTest = fls.readLine();
if (strTest != null && !strTest.startsWith('-')) {
int value = Integer.parseInt(strTest);
// ... you have to store it somewhere
} else {
break;
}
}
You also need some code in there to handle errors including a premature end of file.
I think you should have look at the Scanner API in Java.
You can have a look at their tutorial

Categories