0

So I'm working on this progject that helps build a website for me and I'm looking to read from a folder (that has files) and write to an html document. I did a little research but I always end up having nothing printed or an error. Let me know if there is a way to do this? This is my project and I've changed the folder location and the output to .txt

public class Project {

@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {

    PrintWriter out = new PrintWriter("output.txt");

    Files.walk(Paths.get("C:/Location")).forEach(filePath -> {
        if (Files.isRegularFile(filePath)) {

            out.print(filePath);

            }    
        }); 
    }
}
  • Have you tried printing to console as well? Does anything show up? – saagarjha Aug 15 '15 at 22:50
  • Yes when I print the the console everything shows up. –  Aug 15 '15 at 22:51
  • What are you trying to accomplish with File.isRegularFile(Path p). Are you looking for a file that is not a directory? – lacraig2 Aug 15 '15 at 22:54
  • "output.txt" is probably being made at the root of your hard drive, at C:/, which you may not have write permission to. – saagarjha Aug 15 '15 at 22:55
  • You probably want to use `println` rather than `print`, and you need to close the writer (`out.close()`). – Andreas Aug 15 '15 at 22:56
  • @ILikeTau, we can't assume he is at the root of his hd because he references "C:/Location" instead of just "Location". – lacraig2 Aug 15 '15 at 22:58
  • @lacraig2 This part take some thumbnails that I have and starts to organize them. So it checks to see if it is a picture and it then is suppose to add the name of it to a file so I can then add more information to that file. So I can build part of a website without having to think about. –  Aug 15 '15 at 22:58
  • No, he can get it to output with println, so it's a file writing problem. I think the output.txt is being made somewhere he does not have write access. – saagarjha Aug 15 '15 at 22:59
  • @Andreas I just tried that and it still didn't work :/ –  Aug 15 '15 at 23:01
  • @ILikeTau From what I understand that is my problem. Is there anyway to get around that? –  Aug 15 '15 at 23:01
  • Yes, your file isn't writing. Try using a path to your documents folder, somewhere you know you have write access. – saagarjha Aug 15 '15 at 23:02
  • @ILikeTau How would I do something like that? –  Aug 15 '15 at 23:04
  • @JoetheDailyProgrammer are all the photos in one folder or are their subdirectories? – lacraig2 Aug 15 '15 at 23:05
  • @lacraig2 one folder –  Aug 15 '15 at 23:07
  • @JoetheDailyProgrammer, `System.getProperty("user.home")` points to homedir. Also PrintWriter tends to throw `FileNotFoundException` if it has no write access to provided file... Maybe, problem is with relative paths? – ankhzet Aug 15 '15 at 23:09
  • @ankhzet so I should replace that with the PrintWriter I have? –  Aug 15 '15 at 23:11
  • Yes, make it `PrintWriter out = new PrintWriter(System.getProperty("user.home") + "\\output.txt");` – saagarjha Aug 15 '15 at 23:12
  • @JoetheDailyProgrammer do you need the file name e.g.) output.txt or the entire file path? – lacraig2 Aug 15 '15 at 23:18

2 Answers2

0

Make sure to use PrintWriter("output.txt", true) to autoflush or flush it at the end of your program.

public static void main(String[] args) {
  PrintWriter out = new PrintWriter(System.out, true);
  File dir = new File("images");
  String[] list = dir.list();
  if (list != null) {
    for (String f : list) {
      String[] fileName = f.split("\\.");
      if (fileName.length > 1 && fileName[1].equals("png")) {
        // System.out.println(f);
        out.println(f);
      }

    }
  } else {
    System.out.println("list returned null because dir.canRead is " + dir.canRead());
  }
}

I rewrote a bit of it in the File class instead. Let me know if any of this needs changed. Hope this helps.

lacraig2
  • 655
  • 4
  • 18
  • I'm getting this problem right here Exception in thread "main" java.lang.NullPointerException at Project.main(Project.java:14) –  Aug 15 '15 at 23:19
  • Try running Files.canRead(Your file) to see if you can access it. – lacraig2 Aug 15 '15 at 23:22
  • @JoetheDailyProgrammer try it now. – lacraig2 Aug 15 '15 at 23:27
  • It says I can. Could it be something with Eclipse? –  Aug 15 '15 at 23:28
  • @JoetheDailyProgrammer Try creating a new folder in eclipse (Ctrl N -> select folder). Name it whatever you want. Then change "output.txt" to "whateveryounamedyourfolder/output.txt". Additionally, I would recommend you copy that file to your desktop, change the path, and try it there. – lacraig2 Aug 15 '15 at 23:30
  • Hey thanks for the help, one of the other members ending up getting a little closer to solving it with me; so I gave him the correct answer but I made sure to up-vote your response because it lead us to be closer. Thanks again for the help. –  Aug 16 '15 at 00:01
0

@JoetheDailyProgrammer, you probably writing in wrong file. Try this code:

public static void main(String[] args) throws IOException {

    File out = new File("output.txt");

    System.out.println(out.getAbsolutePath());
}

Run it, and it will show absolute path to yours "output.txt". If it points into different location, than you expected, then use absolute paths in yours app or use users homedir, like this:

public static void main(String[] args) throws IOException {

     File outFile = new File(System.getProperty("user.home") + "/Desktop/output.txt"); // file into users homedir

     System.out.println(outFile.getAbsolutePath()); // print it's location in console

     PrintWriter out = new PrintWriter(outFile); // writer over that file

     Files.walk(Paths.get("C:/Location")).forEach(filePath -> {
         if (Files.isRegularFile(filePath)) {

             out.print(filePath);

             }    
         }); 
     out.close();
    }
}

Upd.

If it's really yours intention to save file into desktop folder, then probably you should navigate it like in the following answers:

https://stackoverflow.com/a/1080900/2109067

https://stackoverflow.com/a/570536/2109067

So:

public static void main(String[] args) throws IOException {
    FileSystemView fsv = FileSystemView.getFileSystemView();
    File outFile = new File(fsv.getHomeDirectory() + "/output.txt"); // file into desktop dir

    System.out.println(outFile.getAbsolutePath()); // print it's location in console
    ...
}
Community
  • 1
  • 1
ankhzet
  • 2,517
  • 1
  • 24
  • 31