Author Topic: Can't read some txt file  (Read 2075 times)

0 Members and 1 Guest are viewing this topic.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Can't read some txt file
« on: March 07, 2015, 03:59:12 pm »
http://pastebin.com/rDD90Nnb#

 
I don't know why some files can be read and some can't.

The main difference I can see, is that small files (2 lines) can be read while larger files can't be.
Is there some sort of limitation on this?

Offline KryDos

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
  • Software Engineer, Emacs uesr
    • View Profile
Re: Can't read some txt file
« Reply #1 on: March 07, 2015, 04:58:37 pm »
I'm awful in java but why are you using fileHandler.hasNext() but then you're trying to get line. Has next it's about token, not about line. Try to use hasNextLine()

And it would be great if you could describe what errors you have
« Last Edit: March 07, 2015, 05:00:56 pm by KryDos »

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Can't read some txt file
« Reply #2 on: March 07, 2015, 05:53:25 pm »
I'm awful in java but why are you using fileHandler.hasNext() but then you're trying to get line. Has next it's about token, not about line. Try to use hasNextLine()

And it would be great if you could describe what errors you have

changing it doesn't fix the problem and the program doesn't crash or anything. It just doesn't bring up the txt file.

Here is an example.
When I open "mapmaking.txt" it displays both links but when I open "test.txt" it doesn't show anything (as if i did nothing).

Offline KryDos

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
  • Software Engineer, Emacs uesr
    • View Profile
Re: Can't read some txt file
« Reply #3 on: March 07, 2015, 06:02:45 pm »
So, why you just not debug the program?

add break point to the hasNext loop and go trough lines step by step.
« Last Edit: March 07, 2015, 06:05:48 pm by KryDos »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Can't read some txt file
« Reply #4 on: March 07, 2015, 09:09:39 pm »
It is an issue with the characterset that the scanner applies implicitly. Has nothing to do with the size of your files, but whether they happen to have the correct charset. If any character of your file is outside of the applied charset's range, the scanner's hasNextLine will return false.
Specify the correct characterset and you are fine, e.g. with

Code: (Java) [Select]
Scanner fileHandler = new Scanner(newFile, "latin1")
your test.txt will work fine.


So, why you just not debug the program?

add break point to the hasNext loop and go trough lines step by step.

This won't help here, because the logic in the code is fine.
Also, that's a pretty lazy answer, you can tell everyone who has issues with a program.
« Last Edit: March 07, 2015, 09:16:47 pm by Deque »

Offline KryDos

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
  • Software Engineer, Emacs uesr
    • View Profile
Re: Can't read some txt file
« Reply #5 on: March 07, 2015, 09:14:16 pm »
This won't help here, because the logic in the code is fine.
Also, that's a pretty lazy answer, you can tell everyone who has issues with a program.

Yeah, as I said I don't know java a lot. This is thing which I could say. Maybe it could be helpful.
And I still think that debug is helpful because OP could find that hasLine() returns false in case if file is still not ended. And then, knowing that hasLine() returns false, OP could write it here and we would try to understand why hasLine() couldreturn false.

But yeah, I will try to be more helpful in future...

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Can't read some txt file
« Reply #6 on: March 07, 2015, 09:18:39 pm »
And I still think that debug is helpful because OP could find that hasLine() returns false in case if file is still not ended. And then, knowing that hasLine() returns false, OP could write it here and we would try to understand why hasLine() couldreturn false.

Indeed, that could help.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Can't read some txt file
« Reply #7 on: March 09, 2015, 09:22:21 pm »
It is an issue with the characterset that the scanner applies implicitly. Has nothing to do with the size of your files, but whether they happen to have the correct charset. If any character of your file is outside of the applied charset's range, the scanner's hasNextLine will return false.
Specify the correct characterset and you are fine, e.g. with

Code: (Java) [Select]
Scanner fileHandler = new Scanner(newFile, "latin1")
your test.txt will work fine.


Yp, it works.
Thank you very much.