문제

Like C language use fgetc() function to read a file characterwise how this can be done in java?? Scanner class reads a file line by line or word by word.. Also someone used a method to take up a word in a String and then use tocharArray to convert into character array.I dont want this solutions.. Do any one have any easy solution like fgetc() does in C??

도움이 되었습니까?

해결책

Beware that what C calls a char, Java calls a byte.

You want to open an InputStream on your file (using Files.newInputStream()). Then, you will .read() from the InputStream. That is the closest thing to C's fgetc().

That is for binary data. If you want to read text instead you'll have to use a Reader.

You may want to have a look here for the full story, because it isn't obvious at all.

다른 팁

Just use the read() method on the InputStream, as long as you don't have multiple objects reading from it (such as a Scanner on the same stream). If you want C-equivalent, nextByte() on the Scanner might be what you're looking for.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top