简体   繁体   中英

how to open all files in a directory in java, read them, creat new files, write to them

I am sorry I dont have a code, I say in a few answers how to make a file and write to it, but I have another question. I give a path to a folder in my compilation, and I want for each file that ends with a .jack to create the same file name that ends with .xml

and open the xml file and write to it. exemple:

start.jack
bet.jack
=>
start.xml
bet.xml

and in each xml file I would like to write stuff according whats written in the jack file.

so actually I need to open the jack file, read from it, and then write to the xml file itself.

I hope I explained myself correctly.

My Code:

public String readFile(String filename)
{
   String content = null;
   File file = new File(filename);
   try {
       FileReader reader = new FileReader(file);
       char[] chars = new char[(int) file.length()];
       reader.read(chars);
       content = new String(chars);
       reader.close();
   } catch (IOException e) {
       e.printStackTrace();
   }
   return content;
}

I took this lines from stackoverflow, and it worked perfectly

File f = new File("your folder path here");// your folder path

//**Edit** It is array of Strings
String[] fileList = f.list(); // It gives list of all files in the folder.

for(String str : fileList){
    if(str.endsWith(".jack")){

        // Read the content of file "str" and store it in some variable

         FileReader reader = new FileReader("your folder path"+str);
        char[] chars = new char[(int) new File("your folder path"+str).length()];
        reader.read(chars);
       String content = new String(chars);
        reader.close(); 

        // now write the content in xml file

         BufferedWriter bw = new BufferedWriter(
         new FileWriter("you folder path"+str.replace(".jack",".xml")));
         bw.write(content); //now you can  write that variable in your file.

         bw.close();
   }
}

List all '.jack' files in a folder:

File folder = new File(path);
File[] files = folder.listFiles(); 

for (File file:files) 
{
    if (file.isFile() && file.getAbsolutePath().endsWith(".jack")) 
    {
        System.out.println(file);
    }
}

Replace extension:

String newPath = file.getAbsolutePath().replace(".jack", ".xml");

Create a new file and write to it:

PrintWriter writer = new PrintWriter("path to your file as string", "UTF-8");
writer.println("Your content for the new file...");
writer.close();

Putting all together:

File folder = new File(path);
File[] files = folder.listFiles(); 

for (File file:files) 
{
    if (file.isFile() && file.getAbsolutePath().endsWith(".jack")) 
    {
        String newPath = file.getAbsolutePath().replace(".jack", ".xml");
        PrintWriter writer = new PrintWriter(newPath, "UTF-8");
        string content = readFile(file.getAbsolutePath());
        // modify the content here if you need to modify it
        writer.print(content);
        writer.close();
    }
}

Lots of good people have put code snippets online for this. Here is one

But, may I ask, why not just rename the file? This link show howto.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM