Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 1.03 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Load text file to memory in Java
  2. File file = new File("wiki.txt");
  3. FileInputStream fileInputStream = new FileInputStream(file);
  4. FileChannel fileChannel = fileInputStream.getChannel();
  5. MappedByteBuffer mapByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
  6. System.out.println((char)mapByteBuffer.get());
  7.        
  8. RandomAccessFile file = new RandomAccessFile("wiki.txt", "r");
  9. FileChannel channel = file.getChannel();
  10. MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1024*50);
  11.        
  12. InputStream is = new BufferedInputStream(new FileInputStream(filename));
  13. byte[] chars = new byte[1024];
  14. int numberOfChars = 0;
  15. while ((numberOfChars = is.read(chars)) != -1)
  16. {
  17.     for (int i = 0; i < numberOfChars; ++i)
  18.     {
  19.         if (chars[i] == 'n' && numberOfChars - i != 1)
  20.         {
  21.             ++count;
  22.         }          
  23.     }
  24. }
  25. count++
  26. return count; // number of lines
  27.        
  28. BufferedReader in = new BufferedReader(new FileReader(fileName));
  29. for (int i = 0; i < endLine; i++)
  30. {
  31.     String oneLine = in.readLine();
  32. }