0

Hi,

I have a rather large binary file that I am attempting to read in, but I only want to read in the first 100 characters or so. I've tried searching the internet for help or an example, but I've only found examples that read in the entire file...and this file is too big to be read in all at once. I tried to split it up by the '\n' character, but it is apparently a continuous string of binary characters. Here is what I have so far - can anyone point me in the right direction? Thank you!

try {
                int c;
                String readInfo = "";
                File myFile = new File("test.txt"); 
                FileReader in = new FileReader(myFile); 
                while ((c = in.read()) != -1) { 
                    readInfo = readInfo + (char)c;
                }
                in.close(); 
                pirateInfo = readInfo.split("\n");
                if (pirateInfo[1] == null){
                    pirateInfo[1].equals("");
                }
                for (int i = 0; i < 100; i++){
                    System.out.println(pirateInfo[i]);
                }
    }
    catch (IOException ioe){
          JOptionPane.showMessageDialog(null, "The file 'test.txt' was not found.");
    }

Again, thank you for any assistance!

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by balmark
0

Try something small/simple like

File f = new File("c:\\myreallybigfile.txt");
      FileInputStream fin = new FileInputStream(f);
      byte[] partial = new byte[1024]; // or whatever
      fin.read(partial);
      String str = new String(partial); // or whatever ;)
      System.out.println(str);
      fin.close();

change to suite and stick in error handling etc

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.