BufferedReader in = new BufferedReader(new FileReader(file));
String line;
int i = 0;
while((line = in.readLine()) != null){
if(line.contains("Time:") == true){
System.out.println(line);
}
i++;
}
System.out.println(i);
in.close();
This code reads file in which is 6213238 lines, but it only reads 1244178. Is there any limits of lines that this class can read or is it with memory usage ? And how to make it work
Edit:
I have print last 18 lines like
while((line = in.readLine()) != null){
if(line.contains("Time:") == true){
System.out.println(line);
}
if(i > 1244160)
{
System.out.println(line);
}
i++;
}
And it showed me 18 last lines from file. I have to say that its strange
EDIT
Problem was with file encoding when I changed UTF-16 to UTF-8, I recive all lines from file
Related
I need to know if I am in the last line of my CSV file or no.
My code is like this:
BufferedReader br = new BufferedReader(new FileReader(MyCSVFile));
while ((line = br.readLine()) != null) {
// Doing some actions
//I need to define here; If it is the last row, do another action
}
Can anyone help me with this?
Thanks
Sin
You could do something like this:
String lastLine = "";
while ((line = br.readLine()) != null) {
// Doing some actions
// Overwrite lastLine each time
lastLine = line;
}
// now do something with lastLine
System.out.println("This is the last line: " + lastLine);
I am using the following bufferedreader to read the lines of a file,
BufferedReader reader = new BufferedReader(new FileReader(somepath));
while ((line1 = reader.readLine()) != null)
{
//some code
}
Now, I want to skip reading the first line of the file and I don't want to use a counter line int lineno to keep a count of the lines.
How to do this?
You can try this
BufferedReader reader = new BufferedReader(new FileReader(somepath));
reader.readLine(); // this will read the first line
String line1=null;
while ((line1 = reader.readLine()) != null){ //loop will run from 2nd line
//some code
}
You can use the Stream skip() function, like this:
BufferedReader reader = new BufferedReader(new FileReader(somepath));
Stream<String> lines = reader.lines().skip(1);
lines.forEachOrdered(line -> {
...
});
File file = new File("path to file");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
int count = 0;
while((line = br.readLine()) != null) { // read through file line by line
if(count != 0) { // count == 0 means the first line
System.out.println("That's not the first line");
}
count++; // count increments as you read lines
}
br.close(); // do not forget to close the resources
Use a linenumberreader instead.
LineNumberReader reader = new LineNumberReader(new InputStreamReader(file.getInputStream()));
String line1;
while ((line1 = reader.readLine()) != null)
{
if(reader.getLineNumber()==1){
continue;
}
System.out.println(line1);
}
You can create a counter that contains the value of the starting line:
private final static START_LINE = 1;
BufferedReader reader = new BufferedReader(new FileReader(somepath));
int counter=START_LINE;
while ((line1 = reader.readLine()) != null) {
if(counter>START_LINE){
//your code here
}
counter++;
}
You can do it like this:
BufferedReader buf = new BufferedReader(new FileReader(fileName));
String line = null;
String[] wordsArray;
boolean skipFirstLine = true;
while(true){
line = buf.readLine();
if ( skipFirstLine){ // skip data header
skipFirstLine = false; continue;
}
if(line == null){
break;
}else{
wordsArray = line.split("\t");
}
buf.close();
I have a comma delimited CSV file ans I have the code to read it from 2nd line. But it gives me the following error.
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
This is my Code.
public static List<String> readCSV(String path){
BufferedReader br = null;
String[] cd = null;
String line ="";
String split=",";
List<String> list = new ArrayList<String>();
try {
br=new BufferedReader(new FileReader(path));
while ((line = br.readLine()) != null) {
boolean firstLine = true;
while (line != null && line.startsWith("child's Last Name")) {
if (firstLine) {
firstLine = false;
continue;
} else {
list.add(line);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}
How can I fix it with this code..
Thanks in Advance!
In your second while loop where are you assigning line again
while (line != null && line.startsWith("child's Last Name"))
Once this condition is satisfied, you are not reassigning it.
Your code should be different something like,
boolean firstLine = true;
while ((line = br.readLine()) != null)
{
if (firstLine)
{
firstLine = false;
continue;
}
else if(line != null && line.startsWith("child's Last Name"))
{
list.add(line);
}
}
try
br=new BufferedReader(new FileReader(path));
boolean firstLine = true;
while ((line = br.readLine()) != null) {
if (line.startsWith("child's Last Name")) {
if (firstLine) {
firstLine = false;
continue;
}
list.add(line);
}
}
There is no need for two while loops
Your problem has nothing to do with the 2nd line; You're simply trying to put the whole file in list and eventually, there's just too much to fit in memory. You have two choices:
Do not keep it all in memory! Process the line, do what you must, and don't store it, so it eventually gets garbage collected
If you really really must keep it all in memory, try increasing the amount of heap space available by starting java with a -Xmx parameter (run java -X for details)
UPDATE: oh oops, yes, as others said, you're also stuck in and endless loop. Consider this answer only once you've fixed that!
Usually, you get an out of memory error when you... well... run out of memory to run the application. Have you debugged into it? You could be stuck in an endless loop...
while (line != null && line.startsWith("child's Last Name")) {
Should this be an if statement?
EDIT:
while ((line = br.readLine()) != null) {
This really should be:
while(br.hasNext()) {
More info: Java - Read CSV with Scanner()
try this..
br=new BufferedReader(new FileReader(path));
br.readLine();
while ((line = br.readLine()) != null) {
if (line.startsWith("child's Last Name")) {
list.add(line);
}
}
My problems is that I have to arrange, when searching for a customer, arrange the while loop to only examine every fourth line read.
This is the code I already have on this problem:
BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;
while ((line = br.readLine()) != null)
{
...
}
br.close();
Does anybody know what needs to be at the place of "..."?
Thanks!
Something along the lines of
int i = 0;
while ((line = br.readLine()) != null)
{
i++;
if (i % 4 == 0)
{
// if i is divisible by 4, then
// your actual code will get executed
...
}
}
Just call br.readLine() 3 times at the end of the loop, discarding the output:
BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;
while ((line = br.readLine()) != null)
{
...
for(int i=0;i<3;i++){ br.readLine(); }
}
br.close();
BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;
int count 0;
while ((line = br.readLine()) != null)
{
if (count!=3)
count++;
else {
// Do something?
count=0;
}
}
br.close();
i am using this code.
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("config.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((br.readLine()) != null) {
temp1 = br.readLine();
temp2 = br.readLine();
}
in.close();
}catch (Exception e){//Catch exception if any
Toast.makeText(getBaseContext(), "Exception", Toast.LENGTH_LONG).show();
}
Toast.makeText(getBaseContext(), temp1+temp2, Toast.LENGTH_LONG).show();
but this is showing exception and is not updating temp1 and temp2.
The exception you see - that I would strongly recommend a) to catch as a specific type, e.g. IOException, and b) to log or show with a message or a stack trace, and c) at least to check for in LogCat, from the DDMS perspective if you are programming with Eclipse - is probably due to Android not finding the config.txt file you are trying to open. Usually, for the simplest cases such as yours, files that are private to an application are opened using openFileInput - see the documentation for details.
Apart from the exception, your reading loop is defective: you need to initialize an empty string before entering, and fill it in the while condition.
String line = "";
while ((line = br.readLine()) != null) {
// do something with the line you just read, e.g.
temp1 = line;
temp2 = line;
}
However, you don't need a loop if you just want to save the first two lines in different variables.
String line = "";
if ((line = br.readLine()) != null)
temp1 = line;
if ((line = br.readLine()) != null)
temp2 = line;
As others have already pointed out, calling readLine consumes a line, so if your config.txt file contains only one line your code consumes it on the while condition, then temp1 and temp2 get null assigned because there's no more text to read.
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("config.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = br.readLine()) != null) {
temp1 = line;
temp2 = line;
}
in.close();
}catch (Exception e){//Catch exception if any
Toast.makeText(getBaseContext(), "Exception", Toast.LENGTH_LONG).show();
}
Toast.makeText(getBaseContext(), temp1+temp2, Toast.LENGTH_LONG).show();
br.readLine() in while already consumes a line.
Try this
LineNumberReader reader = new LineNumberReader(new FileReader("config.txt")));
String line;
while ((line = reader.readLine()) != null) {
//doProcessLine
}
if you want to save the first two lines you have to do:
try
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("config.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = "";
if((line = br.readLine()) != null)
temp1 = line;
if((line = br.readLine()) != null)
temp2 = line;
}
catch(Exception e)
{
e.printStackTrace();
}