0

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

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
Yogi_Bear
  • 512
  • 2
  • 10
  • 24
  • this link might help you http://stackoverflow.com/questions/5751335/using-file-listfiles-with-filenameextensionfilter – rsudha Mar 12 '14 at 09:38
  • you might want to read the Java I/O lesson: http://docs.oracle.com/javase/tutorial/essential/io/ It explains how to do exactly what you want at the Find section: http://docs.oracle.com/javase/tutorial/essential/io/find.html – XSen Mar 12 '14 at 09:43

3 Answers3

3
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();
   }
}
Gunasekar
  • 611
  • 1
  • 8
  • 21
AJ.
  • 4,526
  • 5
  • 29
  • 41
  • this is great! I just dont want to replace a file, I want to create a new file that ends with .xml but thats great, tnx! – Yogi_Bear Mar 12 '14 at 10:03
  • @shine This will create a new file with `.xml` extension. If file is already present then the contents will be overwritten. Thats the nature of `BufferWriter`. – AJ. Mar 12 '14 at 10:04
0

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();
    }
}
ReeCube
  • 2,545
  • 17
  • 23
-1

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.

Hirak
  • 3,601
  • 1
  • 22
  • 33
  • because one is a jack file (a code which is similler to java) and the other is an xml file. and I would need to use both of them. – Yogi_Bear Mar 12 '14 at 09:46
  • so you want to read the content from jack file, create xml content out of it and then write it to the xml file? – Hirak Mar 12 '14 at 09:51
  • This is exactly what I would like to do! – Yogi_Bear Mar 12 '14 at 10:19
  • [How to answer](http://stackoverflow.com/questions/how-to-answer): ***Provide context for links*** *A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.* – BackSlash Mar 12 '14 at 10:25