• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I read multiple files in a single directory?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a Java program that reads a singe text file, modifies it, and then saves it back into the same directory by appending "_Modified.txt" to the original filename. Code is attached below.

I want to modify this code so that instead of entering just a single filename, I instead enter a directory name. The Java program will then look into this directory and modify every file that it finds there. The files will all have the same type of text content and format, but the number of text files found in this directory can vary each time I run the program, and the filenames can be drastically different (not following any naming convention, other than ending with ".txt"). Every file in this directory will need to be modified each time the program is run.

Can someone please tell me how to modify this Java program so that is will look into a single directory, and modify every file that it finds there? As each text file is modified, I want to resave it (as before) by appending "_Modified.txt" to the original filename.
Thanks,
Kevin.


import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Scanner;

class Kevins_File_Manipulator {

public static void main (String args []) throws IOException {
Scanner stdin = new Scanner (System.in);
System.out.print ("File name to read from : ");
String inputFileName = stdin.next();
Scanner sf = new Scanner (new File (inputFileName));
String outputFileName = inputFileName + "_Modified.txt";
System.out.println ("Opening for writing: " + outputFileName);
FileWriter fw = new FileWriter (outputFileName);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter pw = new PrintWriter (bw);
String line;
int count = 0;

// My code goes here that modifies each line of the text file and resaves it to the new file....

pw.close(); // finish and close the output text file

} // end main
} // end class
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

please put your code between the code tags to make it more readable.

What you want to do is create a file object but instead of a file you pass the directory (the check isDirectory() must be true).

You then obtain a File array using file.listFiles() that you can iterate over.
 
Kevin Keinert
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sebastian,
This was my very first post, so please excuse my user unfriendly format. I don't know (yet) how to use code tags to make it more readable.

Also, I'm fairly new to programming.
I will research how to use the Java commands that you suggest. If you have any links to sample code or if you are willing to modify my code as an example to show me, it would be greatly appreciated.

Thanks for your time and help, Kevin.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
welcome to code ranch

I suggest you look at Javadocs

Here is an helpful link revelent to your post:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#list()
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a reminder, even a folder is a file.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic