Hello everyone, I am trying to write to a file what I am currently displaying on the console.

I have a bit of code that works fine by creating a list of files based on it's file extention and it then displays it using println:

What I want it to write that list to a file, rather than display on console.

This is what I am doing.

public class ShowFiles {
 public static void main(String[] args) throws FileNotFoundException {

    List<File> listFiles = ScanFiles.getFileList();

    for (File aList : listFiles) {

            if (aList.isFile() && aList.getName().toLowerCase().endsWith(".txt")
                 || aList.getName().toLowerCase().endsWith(".java"))

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

What do I need to do or how do I need to do it that instead of:

System.out.println(aList);

I get it to write to a File, I already found examples and documents but they always use txt examples and it does not work as I can not do

output.write(text);

unless it is plain text I declared.

Many Thanks in advance.

The aList is java.io.File. When you do:
System.out.println(aList)
What the compiler actually does is, it calls the toString method of the object you passed as argument to the println method. So:
System.out.println(aList.toString())

So my question is. What do you want to save in your file?
The file names? : ScanFiles.getFileList() of this list ?

Also I think that your if should be like this:

if (aList.isFile() && 
[B]([/B]aList.getName().toLowerCase().endsWith(".txt") || aList.getName().toLowerCase().endsWith(".java")[B])[/B]
)

Edited 5 Years Ago by javaAddict: n/a


Hi and thank you for a quick reply, basically the above code I have works perfectly using System.out.println(aList)

This is the output.

C:\>java -cp "JavaApplication.jar" packages.ShowFiles
C:\temp\someText.txt
C:\temp\folder6.5\Image.java
C:\temp\folder6.5\Something.txt
C:\temp\New Text Document.txt
C:\temp\file.java

What I want is for this list to be put into a file, create a file with this output. Do you think you can help me?

Many Thanks

It is like I said. If you have examples that show you how to create and write to file the use that and save what the toString returns:

public class ShowFiles {
 public static void main(String[] args) throws FileNotFoundException {

    List<File> listFiles = ScanFiles.getFileList();
    // open file

    for (File aList : listFiles) {
            if (aList.isFile() && (aList.getName().toLowerCase().endsWith(".txt")
                 || aList.getName().toLowerCase().endsWith(".java"))
)
         String line2Save = aList.toString();
         // write to file
     }

     // close file
  }
}

Hi,

This is what I have and I am trying to get to work but it's a no can do at the moment.

public class LogFiles {
    private static String line2Save;

    public static void main(String[] args) throws FileNotFoundException, IOException{
    Writer output = null;

    List<File> listFiles = ScanFiles.getFileList(); 
    
    for (File aList : listFiles) {
        if (aList.isFile() && (aList.getName().toLowerCase().endsWith(".txt")
                || aList.getName().toLowerCase().endsWith(".java")))
        
    String line2Save = aList.toString();
    
    File file = new File("C:\\temp\\output_file.txt");
    output = new BufferedWriter(new FileWriter(file));
    output.write(line2Save);
    output.close();
    System.out.println("Your file has been written");
    }}
}

There is something wrong with Line 13 it's complaining with (not a statement)

Edited 5 Years Ago by gcclinux: n/a

In your code you wrote:

if (.....) 
  System.out.println(aList)

But now you added more code under the if, so you need:

if (.....) {
  String line2Save = aList.toString();
....
....
}

Also you need to create the BufferedWriter outside the loop. With your way, you create a new instance every time. Just create it once outside the loop, call the the write inside the loop, and outside the loop close it.
Like my example.

Also I would suggest this:

output.write(line2Save);
output.newLine();

Edited 5 Years Ago by javaAddict: n/a

Hi, I have just moved the buffer out side the loop and added the recommended syntax unfortunately my netBeans still complains about:

String line2Save = aList.toString();

(not a statement, expected ;)

and also

output.newLine();

(can not find sybol method newLine)

here is the whole code now including your sugested changes.

public class LogFiles {
   
    private static String line2Save;

    public static void main(String[] args) throws FileNotFoundException, IOException{
    Writer output = null;

    List<File> listFiles = ScanFiles.getFileList(); 
    
    File file = new File("C:\\temp\\output_file.txt");
    output = new BufferedWriter(new FileWriter(file));
    output.write(line2Save);
    output.newLine();
    
    // STARTING FOR LOOP
    
    for (File aList : listFiles) {
        if (aList.isFile() && (aList.getName().toLowerCase().endsWith(".txt")
                || aList.getName().toLowerCase().endsWith(".java")))
        
            String line2Save = aList.toString();
    
            output.write(line2Save);    
    // END FOR LOOP
    }
    output.close();
    System.out.println("Your file has been written");
  }
}

I would like to say thank you so much for the help you giving me. :)

I almost got it working now, but the output looks a bit funny the way it writes to the file.

Post the full code and how your file looks like

For the newLine to work you need:

BufferedWriter output = null;

....

output = new BufferedWriter(new FileWriter(file));

Edited 5 Years Ago by javaAddict: n/a

hello, hope you all had a great weekend. anyway javaAddict you latest coments and suggestion is exacly what I did and it all works and write to file unfortunately it looks like my for loop is the one that is not working in this class, I tell you why I think so:

CLASS:

public class LogFiles {

    public static void main(String[] args) throws FileNotFoundException, IOException{
    BufferedWriter output = null;

    List<File> listFiles = ScanFiles.getFileList();

    File file = new File("C:\\temp\\output_file.txt");
    output = new BufferedWriter(new FileWriter(file));
    
    // STARTING FOR LOOP

    for (File aList : listFiles) {
        if (aList.isFile() && (aList.getName().toLowerCase().endsWith(".txt")
                || aList.getName().toLowerCase().endsWith(".java")))
          
            output.newLine();
            output.write(aList.toString());

    // END FOR LOOP
    }
    output.close();
    System.out.println("Your file has been written");
  }
}

OUT PUT:


C:\temp\file.txtC:\temp\folder6.5
C:\temp\folder6.5\Image.java
C:\temp\folder6.5\Something.txtC:\temp\New Bitmap Image.bmp
C:\temp\New Text Document.txt
C:\temp\output_file.txt

As you can see it logs to file as expected and for each (txt and java) file it finds it writes to file and do a new line, unfortunately for the files and directories that are not part of the for loop it still adds it concatinating it to the existing line.

Take a close look at Line 1 and 3 it is still adding it to the output.

I am going to continue looking into this and I will let you know if I find a solution.

Regards,

Edited 5 Years Ago by gcclinux: n/a

Problem sorted as I was missing a { } in the for loop after the if statement.

UPDATED CODE:

if (aList.isFile() && aList.getName().toLowerCase().endsWith(".txt")
                || aList.getName().toLowerCase().endsWith(".java"))[B]{[/B]
                output.newLine();
                output.write(aList.toString());
        [B]}[/B]

OUTPUT TO FILE

C:\temp\file.txt
C:\temp\folder6.5\Image.java
C:\temp\folder6.5\Something.txt
C:\temp\java\LogFiles.java
C:\temp\New Text Document.txt

This article has been dead for over six months. Start a new discussion instead.