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;
                pirateInfo = readInfo.split("\n");
                if (pirateInfo[1] == null){
                for (int i = 0; i < 100; i++){
    catch (IOException ioe){
          JOptionPane.showMessageDialog(null, "The file 'test.txt' was not found.");

Again, thank you for any assistance!

Try something small/simple like

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

change to suite and stick in error handling etc

This article has been dead for over six months. Start a new discussion instead.