Page 1 of 3 123 LastLast
Results 1 to 20 of 52
  1. #1
    Join Date
    May 2014
    Posts
    25
    Rep Power
    0

    Default Scanning non-txt files with unspecified filename

    Hello!

    I want to make a tool that does the 2 following things::

    - Open files that are not .txt but can be opened as .txt and return them as a string. It just returns an empty string at the moment.
    - The filenames are unknown, just the file extension at the end and the the YYYYMMDD number in front are always the same, therefore I'd like the app to simply scan every file in the same folder (not the same file twice, obviously). How can this be done?

    That's what I've got so far.
    Java Code:
        public String readFile(String filename) throws FileNotFoundException {
    		Scanner scanner = null;
    		File file = new File(filename);
    		String output = "";
    		try{
    			scanner = new Scanner(file);
    		}catch(FileNotFoundException e){
    			System.out.println("Error: File " + filename + " not found!");
    		}
    		while (scanner.hasNextLine()){
    			output=output+scanner.nextLine();
    		}
    		return output;
    	}

    Regards,
    UAIU

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Scanning non-txt files with unspecified filename

    You don't show what happens to that output.
    Also, if you're doing that sort of appending then you should be using a StringBuilder.

    Debug the above method by printing out the value of 'output' just before it returns it to the calling code. Pick a really small file of only a couple of lines.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    May 2014
    Posts
    25
    Rep Power
    0

    Default Re: Scanning non-txt files with unspecified filename

    It's just returning it to me, I'm using bluej to test the methods.

    "Debug the above method by printing out the value of 'output' just before it returns it to the calling code."
    Returned nothing, just an empty line.


    It's working fine with ordinary .txt files.
    Last edited by UsernameAlreadyInUse; 05-15-2014 at 10:20 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Scanning non-txt files with unspecified filename

    Do some more debugging: does the hasNextLine() method ever return true? The file may not have a end line character.
    Last edited by Norm; 05-15-2014 at 05:44 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    May 2014
    Posts
    25
    Rep Power
    0

    Default Re: Scanning non-txt files with unspecified filename

    No, it never actually gets into the loop. No idea what to do now, google didn't help either.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Scanning non-txt files with unspecified filename

    non-text files probably do NOT have endline characters. So hasNextLine() wouldn't return true.
    The Scanner class is for text files. Look at using another class to read non-text files.

    What is in the file? Can you look at the file with a hex editor?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    May 2014
    Posts
    25
    Rep Power
    0

    Default Re: Scanning non-txt files with unspecified filename

    I've got no hexeditor. I can give you one of the files, though, if that'd help.
    Attached Files Attached Files

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Scanning non-txt files with unspecified filename

    What do you see if you open the file in a text editor? Is everything on one line?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Scanning non-txt files with unspecified filename

    It's a zip file, so not sure what you're expecting to manage to read out of it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Join Date
    May 2014
    Posts
    25
    Rep Power
    0

    Default Re: Scanning non-txt files with unspecified filename

    Quote Originally Posted by Norm View Post
    What do you see if you open the file in a text editor? Is everything on one line?
    No, it's not just one line, but the first line is huge. Simply adding a bunch of paragraphs to shorten the lines and saving the file just for testing purposes doesn't make my method work, though. So that doesn't seem to be the problem?


    It's a zip file, so not sure what you're expecting to manage to read out of it.
    The file type isn't supported so I had to zip it. I want to read the file that is in the zip (the first "line" in particular).

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Scanning non-txt files with unspecified filename

    Why do you think a file that does not contain text can be read as text?
    What text would you expect when the code reads an image file like .gif or .png?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Scanning non-txt files with unspecified filename

    This is beginning to sound like an X-Y Problem...

    What would you like to achieve by reading this file?

    Note that there are different ways to read the contents of a file depending on the type of file. See Lesson: Basic I/O (The Java™ Tutorials > Essential Classes) to familiarise yourself with your options.

    Is this a World of Tanks data file? If it is, then take a look at Projects and Tools - WoT Developer Wiki. There are scripts/applications available to convert the binary data file into text-based JSON format, which you can then read just like any other text file.

  13. #13
    Join Date
    May 2014
    Posts
    25
    Rep Power
    0

    Default Re: Scanning non-txt files with unspecified filename

    Quote Originally Posted by Norm View Post
    Why do you think a file that does not contain text can be read as text?
    What text would you expect when the code reads an image file like .gif or .png?
    I was assuming I could somehow open the file and get the same content the texteditor gets when you open it. It's no cryptic nonsense, it's perfectly readable text.


    Quote Originally Posted by Norm View Post
    Is this a World of Tanks data file? If it is, then take a look at Projects and Tools - WoT Developer Wiki. There are scripts/applications available to convert the binary data file into text-based JSON format, which you can then read just like any other text file.
    I have actually found that a couple of minutes ago as well. How can I incorporate that python script into my java application? Have never done anything like that before.
    Last edited by UsernameAlreadyInUse; 05-15-2014 at 07:27 PM.

  14. #14
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Scanning non-txt files with unspecified filename

    Quote Originally Posted by UsernameAlreadyInUse View Post
    I have actually found that a couple of minutes ago as well. How can I incorporate that python script into my java application? Have never done anything like that before.
    If it comes with just a Python script, then you'll need to first install a Python interpreter before the script can be executed. Take a look if it also comes with a .exe file (which would be directly executable on Windows without needing to install the interpreter.)

    Once you have sorted out the above, try and convert the data file manually first using the script/application downloaded from the WoT Developer Wiki. This is so that you know the exact manual steps involved, which would then inform you what you need to do to automate this in Java.

    In Java, executing an external script/application typically involves using Runtime.exec() or ProcessBuilder. See When Runtime.exec() won't | JavaWorld and How to run external programs by using Java ProcessBuilder class? for further information on these.

    I almost forgot... depending on how the Python script is written, you may also be able to run it directly in the JVM using Java's Scripting API. See http://www.drdobbs.com/jvm/jsr-223-s...form/215801163 for further info.
    Last edited by jashburn; 05-15-2014 at 07:55 PM. Reason: Add paragraph on Java's Scripting API

  15. #15
    Join Date
    May 2014
    Posts
    25
    Rep Power
    0

    Default Re: Scanning non-txt files with unspecified filename

    Quote Originally Posted by jashburn View Post
    If it comes with just a Python script, then you'll need to first install a Python interpreter before the script can be executed. Take a look if it also comes with a .exe file (which would be directly executable on Windows without needing to install the interpreter.)

    Once you have sorted out the above, try and convert the data file manually first using the script/application downloaded from the WoT Developer Wiki. This is so that you know the exact manual steps involved, which would then inform you what you need to do to automate this in Java.

    In Java, executing an external script/application typically involves using Runtime.exec() or ProcessBuilder. See When Runtime.exec() won't | JavaWorld and How to run external programs by using Java ProcessBuilder class? for further information on these.

    I almost forgot... depending on how the Python script is written, you may also be able to run it directly in the JVM using Java's Scripting API. See JSR 223: Scripting for the Java Platform | Dr Dobb's for further info.
    I think I read something about an exe version of that script. I will try to do everything you wrote later tonight, thanks.

  16. #16
    Join Date
    May 2014
    Posts
    25
    Rep Power
    0

    Default Re: Scanning non-txt files with unspecified filename

    I moved to my windows partition to be able to execute the exe and my original method turned out to be able to read the file just fine without any additions.


    So how can I tell it to read all the files with the same file extension in a folder? The names are all different.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Scanning non-txt files with unspecified filename

    all the files with the same file extension in a folder
    Look at the File class. It has methods for getting a list of all the files in a folder.
    Also their are filtering classes that will help restrict what goes in the list.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Join Date
    May 2014
    Posts
    25
    Rep Power
    0

    Default Re: Scanning non-txt files with unspecified filename

    Thanks, I will do that later, because I've already started with the extraction of the details I want. I encountered a problem, though.


    2 replays I tested won't give me a string longer than 4096 and 2 other replays have the same problem but they're longer (32768). Any idea what is causing this?


    Java Code:
    public void readReplay(String filename) throws FileNotFoundException {
            Scanner scanner = null;
            File file = new File(filename);
            StringBuilder output = new StringBuilder();
            try {
                scanner = new Scanner(file);
            }catch(FileNotFoundException e) {
                System.out.println("Error: File " + filename + " not found!");
            }
            while (scanner.hasNextLine()) {
                output.append(scanner.nextLine());
            }
            System.out.println(output.toString());
            return output.toString();
        }
    Pretty much the same as before.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Scanning non-txt files with unspecified filename

    Any idea what is causing this?
    The endline characters are not at the correct locations in the file. The nextLine() method reads all the bytes up to the endline character.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Join Date
    May 2014
    Posts
    25
    Rep Power
    0

    Default Re: Scanning non-txt files with unspecified filename

    It just stops reading totally randomly, though? Like in the middle of a word, e.g.:
    "killerID": 0,

    And wouldn't it then simply append the next line to the string anyway? Does it even matter where the scanner starts with a new line?
    Last edited by UsernameAlreadyInUse; 05-17-2014 at 03:51 PM.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 6
    Last Post: 10-11-2011, 09:48 PM
  2. Output Jar Filename
    By CuddlyKittens11 in forum NetBeans
    Replies: 0
    Last Post: 04-25-2011, 01:09 AM
  3. Replies: 2
    Last Post: 09-14-2010, 01:24 AM
  4. Creating unspecified ammount of objects
    By plm-pusik in forum New To Java
    Replies: 3
    Last Post: 09-01-2010, 06:46 PM
  5. HashMap with unspecified value type
    By kleelof in forum New To Java
    Replies: 16
    Last Post: 04-28-2010, 12:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •