Trying to read binary file as text but scanner stops at first line

后端 未结 3 1596
野的像风
野的像风 2021-01-27 02:56

I\'m trying to read a binary file but my program just stops at first line.. I think it\'s because of the strange characters the file has..I just want to extract some directions

相关标签:
3条回答
  • 2021-01-27 03:36

    You cannot use a line-oriented scanner to read binary files. You have no guarantee that the binary file even has "lines" delimited by newline characters. For example, what would your scanner do if there were TWO files matching the pattern "D:\.*.mp3" with no intervening newline? You would extract everything between the first "D:\" and the last ".mp3", with all the garbage in between. Extracting file names from a non-delimited stream such as this requires a different strategy.

    If i were writing this I'd use a relatively simple finite-state recognizer that processes characters one at a time. When it encounters a "d" it starts saving characters, checking each character to ensure that it matches the required pattern, ending when it sees the "3" in ".mp3". If at any point it detects a character that doesn't fit, it resets and continues looking.

    EDIT: If the files to be processed are small (less than 50mb or so) you could load the entire file into memory, which would make scanning simpler.

    0 讨论(0)
  • 2021-01-27 03:43

    As was said, since it is a binary file you can't use a Scanner or other character based readers. You could use a regular FileInputStream to read the actual raw bytes of the file. Java's String class has a constructor that will take an array of bytes and turn them into a string. You can then search that string for the file name(s). This may work if you just use the default character set.

    String(byte[]): http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html FileInputStream for reading bytes: http://download.oracle.com/javase/tutorial/essential/io/bytestreams.html

    0 讨论(0)
  • 2021-01-27 03:44

    Use hasNextLine() instead of hasNext() in the while loop check.

    while (readF.hasNextLine()) {
     String line = readF.nextLine();
     //Your code
     }
    
    0 讨论(0)
提交回复
热议问题