BufferedReader skip lines assets file java android - java
I'm trying to read a .txt-file that's located in my assets folder in an Android project. Search the file, read the file with the InputStreamer and the BufferedReader works just fine, but the problem is: it doesn't read ALL the lines. So when I want to add a line to an ArrayList for further use, not all the lines are present in this list. This is my code:
InputStream inputStream;
BufferedReader br;
try {
inputStream = getResources().getAssets().open("KeyMapping.txt");
br = new BufferedReader(new InputStreamReader(inputStream));
final ArrayList<String> viewList = new ArrayList<String>();
String line = null;
//Add every line (except the first) to an arrayList
while ((line = br.readLine()) != null && (line = br.readLine()) != "1,2,3,4,5,6,7,8,char") {
viewList.add(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
The format of my .txt-file is like this:
1,2,3,4,5,6,7,8/char
1,,,,1,,,/a
2,,,,1,,,/b
3,,,,1,,,/c
,1,,,1,,,/d
,2,,,1,,,/e
,3,,,1,,,/f
,,1,,1,,,/g
,,2,,1,,,/h
,,3,,1,,,/i
,,,,1,,,1/j
,,,,1,,,2/k
,,,,1,,,3/l
1,,,,2,,,/m
2,,,,2,,,/n
3,,,,2,,,/o
,1,,,2,,,/p
,2,,,2,,,/q
,3,,,2,,,/r
,,1,,2,,,/s
,,2,,2,,,/t
,,3,,2,,,/u
,,,,2,,,1/v
,,,,2,,,2/w
,,,,2,,,3/x
...
Only a few of these lines will be added to the ArrayList, does anyone know why?
while ((line = br.readLine()) != null && (line = br.readLine()) != "1,2,3,4,5,6,7,8,char")
This line reads 2 lines from the stream, and processes the 2nd line always.
Also, you cannot compare strings with != operator. Use String.equals() method.
while ((line = br.readLine()) != null)
{
if(line.equals("1,2,3,4,5,6,7,8,char"))
continue;
//add if it's not that string
viewList.add(line);
}
Related
Getting data from php file in my Android java class using bufferreader
I am getting data from php file in android java class using BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));. This code is in my php file is echo "abc"; echo "xyz"; This is the code of my java file. BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream())); String line = null; Complete_line = null;// Read Server Response while ((line = reader.readLine()) != null) { System.out.println(line); break; } But when I read from Buffer reader it will read a whole one line, or it will print "line" string as "abcxyz". But I want them as two lines as they are two different lines in the PHP file.
try this, BufferedReader reader = null; StringBuffer response = new StringBuffer(); try { reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { response.append(line+"\n"); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
In PHP, unless you seperate the 2 lines a a newline character \n, the two echos are the same. To seperate echos into different lines, you need to end the strings with \n, like this: echo "abc\n"; echo "xyz\n";
String from bufferedReader to arraylist
I have a local server-class, which sends two String messages ("The first message", "The second message") to client-class. In client-class I'd like to put them into an ArrayList code from BufferedReader and then print. void go () throws IOException { Socket socket = new Socket("127.0.0.1",4242); InputStreamReader streamReader = new InputStreamReader(socket.getInputStream()); BufferedReader reader = new BufferedReader(streamReader); ArrayList <String> list = new ArrayList<String>(); while (reader.readLine() != null) { list.add(reader.readLine()); } System.out.println(list); } But all that I get - is a line "null" What's wrong with it? I’ll be very grateful for any help
ciprianoss is correct but also in the loop while (reader.readLine() != null) { list.add(reader.readLine()); } You are reading from the file TWICE Once in the while and the again just before you add. list.add(reader.readLine()); as ciprianoss suggested String line; while( (line = reader.readLine()) != null) { list.add(line); {
Once you do .readLine() it will no longer be in the buffer, change your while loop to: String line = null; while((line = reader.readLine()) != null) { list.add(line); }
Easiest way should be using bufferedReader.lines(), which returns a stream: public List<String> readerToList(final Reader reader) { try (final BufferedReader bufferedReader = new BufferedReader(reader)) { return bufferedReader.lines().collect(Collectors.toList()); } }
using BufferedReader in Java
How can I use BufferedReader for reading all lines between two concrete line. for example i want to start reading from line1 то line2, can I use this code BufferedReader reader = new BufferedReader(new FileReader("file.txt")); String line1 = "StartLine"; String line2 = "EndLine"; while (!line1.equals(line2)) { // do something line1 = reader.readLine(); } I write something like this, but it does not working! Please help me, I am beginner in Java!
Change the loop in following way. You need to read the line in condition and check if it's not nutll i.e. end of file. String line1 = "StartLine"; String line2 = "EndLine"; String line3 = null; //Iterate upto line1 while( (line3 = reader.readLine()) != null && ! line3.equals(line1)); //Print the lines till line2 while(line3 != null && ! line3.equals(line2) ) { System.out.println(line3); line3 = reader.readLine(); }
First you need another loop to read all lines BEFORE your "StartLine" and then you will need to be more specific about what is NOT working. What you expect to happen and what is not happening
You can try using BufferReader's mark and reset methods: BufferedReader reader = new BufferedReader(new FileReader("file.txt")); String line1 = "StartLine"; String line2 = "EndLine"; String line; while ((line = reader.readLine()) != null) { if (line1.equals(line)) { System.out.println(line); reader.mark(100); } } reader.reset(); while ((line = reader.readLine()) != null) { if (line2.equals(line)) { break; } else { System.out.println(line); //or whatever you want to do } } } You might have to play with the mark value, but something like this might work for you. Hope that helps.
BufferedReader() returning empty string when there are more strings in the file?
My txt file looks like this: data;data2;data3;data4..........up till data3146 When I open the txt file in notepad I see it in the form given above. But when I copy paste the first few lines to another place, There is a 1 line gap b/w data1 and everything else. Because of this I am getting problems while accessing the file in Java and using the data with a bufferedreader in a loop. How can I correct this? I can't remove the empty line as it is not even visible in the original file.
You can ignore the blank line(s). Something like this - while ((line = reader.readLine()) != null) { if(line.trim().isEmpty()) { continue; } ...
you can try this way: BufferedReader reader = new BufferedReader(new FileReader(new File("your file path"))); String str = null; while((str = reader.readLine())!=null) { if (str.matches("[' ']+")) { continue; } else { // to do } }
I believe that the problem is in the line endings. Basically you can skip the empty lines: String line; while ((line = reader.readLine()) != null) { if ("".equals(line.trim()) { continue; } // do your stuff here }
How to escape a line that starts with a special character while reading files in java
Suppose a file contains the following lines: #Do #not #use #these #lines. Use these. My aim is to read only those lines which does not start with #. How this can be optimally done in Java?
Let's assume that you want to accumulate the lines (of course you can do everything with each line). String filePath = "somePath\\lines.txt"; // Lines accumulator. ArrayList<String> filteredLines = new ArrayList<String>(); BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new FileReader(filePath)); String line; while ((line = bufferedReader.readLine()) != null) { // Line filtering. Please note that empty lines // will match this criteria! if (!line.startsWith("#")) { filteredLines.add(line); } } } finally { if (bufferedReader != null) bufferedReader.close(); } Using Java 7 try-with-resources statement: String filePath = "somePath\\lines.txt"; ArrayList<String> filteredLines = new ArrayList<String>(); try (Reader reader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(reader)) { String line; while ((line = bufferedReader.readLine()) != null) { if (!line.startsWith("#")) filteredLines.add(line); } }
Use the String.startsWith() method. in your case you would use if(!myString.startsWith("#")) { //some code here }
BufferedReader.readLine() return a String. you could check if that line starts with # using string.startsWith() FileReader reader = new FileReader("file1.txt"); BufferedReader br = new BufferedReader(reader); String line=""; while((line=br.readLine())!=null){ if(!line.startsWith("#")){ System.out.println(line); } } }