1 2 Previous Next 21 Replies Latest reply: May 9, 2008 5:10 PM by 807591 RSS

    Question about File class - I'm stumped!

    807591
      here's a simplified version of my code (removal of try/catch statements)
      String file = "C:/Users/Student/Pictures/IGN/Board scanner/threads.txt";
      File realfile = new File(file);
      scnr = new Scanner(file);
      if(scnr.hasNext())
      System.out.println("true");
      else
      System.out.println("false");
      I run the program and it's printing false, but the file is a real file and it contains text, I can open it in Windows explorer and look at it. So I decided to use the debugger and found that the File had converted the String I inputted to be
      String example = "C:\\Users\\Student\\Pictures\\IGN\\Board scanner\\threads.txt"
      I think this may be the problem, because I can WRITE to the file using BufferedWriter just fine by inputing the String file (seen above), but I can't READ from the file because the File class makes it abstract...

      Any way to fix this?

      Edited by: SideShowMel0329 on May 9, 2008 12:44 PM
        • 1. Re: Question about File class - I'm stumped!
          807591
          SideShowMel0329 wrote:
          String file = "C:/Users/Student/Pictures/IGN/Board scanner/threads.txt";
          scnr = new Scanner(file);
          I don't know if that's a bug -- you are not parsing the contents of threads.txt, but you are parsing the string "C:/Users/Student/Pictures/IGN/Board scanner/threads.txt".
          • 2. Re: Question about File class - I'm stumped!
            807591
            Did you try passing the Scanner the File object and not the String?
            scnr = new Scanner(realfile);
            • 3. Re: Question about File class - I'm stumped!
              807591
              Ahhh!!! Typo Typo!!! I was simplifying the code and there was a typo

              What really happens is that I take realfile and then pass it in a function
              public static void collectThread(File s) {
              File file = s;
              Scanner scnr = new Scanner(file);
              Is a problem occuring at File file = s; ?
              • 4. Re: Question about File class - I'm stumped!
                807591
                I think you need to post a SSCCE: [http://mindprod.com/jgloss/sscce.html]
                • 5. Re: Question about File class - I'm stumped!
                  807591
                  public class Main {
                  BufferedWriter out;
                  String file = "C:/Users/Student/Pictures/IGN/Board scanner/threads.txt";
                  File realfile;
                       try {
                            realfile = new File(file);
                            out  = new BufferedWriter(new FileWriter(realfile));
                  
                  oldUsers = new TreeMap<String, Integer>(Reader.collectThread(realfile));
                  
                  Iterator itr = oldUsers.keySet().iterator();
                  while(itr.hasNext()) {
                       String holder = (String)itr.next();
                       out.write("[link=http://club.ign.com/b/about?username=" + holder + "]" + holder + "[/link]");
                       out.write(" ");
                       out.write("" + oldUsers.get(holder));
                       out.newLine();
                  }
                  
                  
                  
                  public static TreeMap<String, Integer> collectThread(File s) {
                            Scanner scnr;
                            TreeMap<String, Integer> users = new TreeMap<String, Integer>();
                            // create File
                            File file = s;
                            try {
                                 scnr = new Scanner(file);
                            
                            //Start retreiving
                            String holder;
                            if(scnr.hasNext())
                                 holder = scnr.next();
                            else {
                                 System.out.println("No old map");
                                 return users;
                            }
                            System.out.println("Getting old Map...");
                            
                            while(scnr.hasNext()) {
                                 users.put(holder, Integer.parseInt(scnr.next()));
                                 holder = scnr.next();
                            }
                            } catch (FileNotFoundException e) {
                                 e.printStackTrace();
                            }
                            return users;
                       }
                  • 6. Re: Question about File class - I'm stumped!
                    807591
                    Basically, collectTread is returning an empty TreeMap every time, even though there's code (properly formatted code) in the File at the time.
                    • 7. Re: Question about File class - I'm stumped!
                      807591
                      Your code doesn't compile.
                      • 8. Re: Question about File class - I'm stumped!
                        807591
                        Nevermind then, there's a lot of excess code and it's so hard trimming through it all...

                        I've already told you guys what the problem is, the File class is converting the path name to something with "\\" instead of "\" or "/" and I think that's causing it to trip up...

                        Anyways, thanks
                        • 9. Re: Question about File class - I'm stumped!
                          807591
                          SideShowMel0329 wrote:
                          Nevermind then, there's a lot of excess code and it's so hard trimming through it all...

                          I've already told you guys what the problem is, the File class is converting the path name to something with "\\" instead of "\" or "/" and I think that's causing it to trip up...

                          Anyways, thanks
                          That's not a bug, that's a feature: you can use "/" as a platform independent path separator. File converts the path to the proper form for your OS.
                          • 10. Re: Question about File class - I'm stumped!
                            807591
                            *************UPDATE***************

                            Some code you can run to help fix the problem:
                            BufferedWriter testout;
                                           File testfile;
                                           Scanner testscnr;
                                           
                                           
                                           try {
                                                
                                                testfile = new File("test.txt");
                                                testout = new BufferedWriter(new FileWriter(testfile));
                                                
                                                
                                                testout.write("test");
                                                testout.close();
                                                
                                                /*
                                                testscnr = new Scanner(testfile);
                                                String s = "new test: " + testscnr.next();
                                                
                                                testout.write(s);
                                                testout.close();
                                                */
                                                } catch (IOException e) {
                                                     e.printStackTrace();
                                                }
                            First, run this. Then, check your local project file to ensure that a file "test.txt" exists and that it has a string inside that says "test".

                            Then, run this:
                            BufferedWriter testout;
                                           File testfile;
                                           Scanner testscnr;
                                           
                                           
                                           try {
                                                
                                                testfile = new File("test.txt");
                                                testout = new BufferedWriter(new FileWriter(testfile));
                                                
                                                /*
                                                testout.write("test");
                                                testout.close();
                                                */
                                                
                                                testscnr = new Scanner(testfile);
                                                String s = "new test: " + testscnr.next();
                                                
                                                testout.write(s);
                                                testout.close();
                                                
                                                } catch (IOException e) {
                                                     e.printStackTrace();
                                                }
                            This should take in the String from the file, and rewrite to the file "new test: test"

                            But instead, I get this exception:

                            Exception in thread "main" java.util.NoSuchElementException
                                 at java.util.Scanner.throwFor(Scanner.java:838)
                                 at java.util.Scanner.next(Scanner.java:1347)
                                 at Main.main(Main.java:130)
                            • 11. Re: Question about File class - I'm stumped!
                              807591
                              Your example code should be complete: ready to be copied, compiled and run. Every further task you put on forum members means that fewer and fewer will bother to look at it.

                              But I took your code, made it into an example and it works for me:
                              import java.io.*;
                              import java.util.*;
                              
                              public class ScannerTest {
                                  public static void main(String[] args) throws IOException {
                                      File file = new File("test.txt");
                                      writeTestFile(file);
                                      scanTestFile(file);
                                  }
                              
                                  static void writeTestFile(File file)  throws IOException {
                                      BufferedWriter out = new BufferedWriter(new FileWriter(file));
                                      out.write("test");
                                      out.close();
                                  }
                              
                                  static void scanTestFile(File file)  throws IOException {
                                       Scanner scanner = new Scanner(file);
                                       while (scanner.hasNext()) {
                                           System.out.println("[" + scanner.next() + "]");
                                       }
                                       scanner.close();
                                  }
                              }
                              • 12. Re: Question about File class - I'm stumped!
                                807591
                                That was helpful (thanks for the rewrite), but I need the second function to write to the file, not System.out.println
                                • 13. Re: Question about File class - I'm stumped!
                                  807591
                                  SideShowMel0329 wrote:
                                  That was helpful (thanks for the rewrite), but I need the second function to write to the file, not System.out.println
                                  You're not implying that you want to read from and write to the same file concurrently?
                                  • 14. Re: Question about File class - I'm stumped!
                                    807591
                                    No, that's why I instructed you to run the main twice.

                                    In your example, you would run it the first time with the second method call commented out, then after it's done, run it again with the first method call commented out...

                                    Except in your second method call instead of println I need out.write()
                                    1 2 Previous Next