Results 1 to 10 of 10
Thread: Load file with a lot or lines.
- 04-25-2011, 03:11 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 25
- Rep Power
- 0
- 04-25-2011, 03:16 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2011, 03:28 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 25
- Rep Power
- 0
Thanks Jos.. so I am wondering if there is a way to do that much faster, read the file line by line will take a lot of time and the process needs to be as fast as possible.
- 04-25-2011, 03:30 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2011, 03:38 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 25
- Rep Power
- 0
I'll review the problem again.
Thanks friend.
- 04-25-2011, 04:14 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 22
- Rep Power
- 0
Try:
int size = (int)f.length();
byte[] bytes = new byte[size];
InputStream in = new BufferedInputStream(new FileInputStream(f));
in.read(bytes);
Be aware that the maximum number of bytes in the file can be no more than Integer.MAX_VALUE, and you may have to up the memory of your app.
Or if you want super efficency:
FileChannel fc = new FileInputStream(f).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(size);
fc.read(buffer);
Both these methods will read the whole file into memory and then you can mess around with it to your hearts content.Last edited by Jeeni101; 04-25-2011 at 04:17 PM.
- 04-25-2011, 04:44 PM #7
Member
- Join Date
- Apr 2010
- Posts
- 25
- Rep Power
- 0
Thanks a lot.
- 04-25-2011, 11:51 PM #8
Member
- Join Date
- Apr 2010
- Posts
- 25
- Rep Power
- 0
Load file with a lot of lines.
I loaded the file using getChannel it looks like this:
It let me load the file and then in the while loop show the content of it, but I need to take the file line by line, so I tried to add a clause if like this:Java Code:FileInputStream f = new FileInputStream(this.txtArchivo.getText()); FileChannel inChannel = f.getChannel(); int bytesRead = 200; ByteBuffer buffer = ByteBuffer.allocate(200); while (bytesRead != -1) { buffer.flip(); while (buffer.hasRemaining()) { char c = (char) buffer.get(); int valor = (int) c; System.out.print(c); } buffer.clear(); bytesRead = inChannel.read(buffer); } f.close();
But it did not work.Java Code://valor stores ascii code of caracter read if (valor != 13){ //add the char to a string builder object }
Any suggestions ?
Greetings!!
- 04-26-2011, 06:52 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 22
- Rep Power
- 0
Hope this helps
Try this:
The biggest differance is that I used '\n' as the CR charactor rather than testing for the ascii value. '\n' should be more reliable.
Other things to note are the sizing of the buffer.
I'm not sure where you are trying to get to, but you could conver the buffer to a String and use the replace method to replace CRs with white space. Thats the commented out part.
File file = new File("C:/Temp/big/BigFile.txt");
long size = file.length();
if(size > Integer.MAX_VALUE) {
System.out.println("File to big");
System.exit(0);
}
final int maxBufferSize = 1048576;
long bufferSize = size > maxBufferSize ? maxBufferSize : size;
FileInputStream f = new FileInputStream(file.getAbsolutePath());
FileChannel inChannel = f.getChannel();
int bytesRead = 0; // No bytes read yet!!
// Note that bufferSize can only be a maximum of megaByte as par the inialisation above.
ByteBuffer buffer = ByteBuffer.allocate((int)bufferSize);
int crCount = 0; // Just a variable to count the carriage return characters.
bytesRead = inChannel.read(buffer);// To prime things before going into the loop
while (bytesRead != -1) {
buffer.flip();
// int bytesInBuffer = buffer.limit();
// byte[] bufferBytes = new byte[bytesInBuffer];
// buffer.get(bufferBytes);
// String bufferContentsWithNoCarriageReturns = new String(bufferBytes).replace('\n', ' ');
// System.out.println(bufferContentsWithNoCarriageRet urns);
while (buffer.hasRemaining()) {
char valor = (char) buffer.get();
if (valor != '\n'){
//add the char to a string builder object
} else {
crCount++;
}
}
buffer.clear();
bytesRead = inChannel.read(buffer);
}
f.close();
System.out.println("Done. Carriage Returns Found: " + crCount);
Oh, and by the way, the slowness you may be finding could be down to the processing of the Strings when you get your hands on the lines in the file.Last edited by Jeeni101; 04-26-2011 at 07:00 PM.
- 04-26-2011, 07:41 PM #10
Member
- Join Date
- Apr 2010
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
Two '\n' characters between lines while reading File
By subith86 in forum New To JavaReplies: 6Last Post: 02-26-2011, 09:56 AM -
how to remove duplicate lines from a file
By trkece in forum New To JavaReplies: 11Last Post: 02-16-2011, 06:01 AM -
How to remove 2 last lines in a text file?
By Marius in forum New To JavaReplies: 2Last Post: 11-30-2008, 03:54 PM -
Remove duplicate lines from a text file
By Dirt.Diver in forum New To JavaReplies: 15Last Post: 06-25-2008, 02:08 PM -
How to get the count of all the lines in a file
By Java Tip in forum java.ioReplies: 0Last Post: 04-06-2008, 07:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks