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!

Recommended Answers

All 2 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.