2014-03-12 42 views
0

对不起,我没有代码,我在几个答案中说如何制作文件并写入它,但我有另一个问题。 我给出了一个在我的编译中的文件夹的路径,我希望以.jack结尾的每个文件创建以.xml结尾的相同文件名,然后打开xml文件并写入.xml文件。 为例:如何在java中打开目录中的所有文件,读取它们,创建新文件,写入它们

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

,并在每一个XML文件,我想根据什么写在千斤顶文件写的东西。

所以实际上我需要打开jack文件,从它读取,然后写入xml文件本身。

我希望我能正确解释自己。

我的代码:

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; 
} 

我把这个线从计算器,和它的工作完美

+0

这个链接可以帮助你 http://stackoverflow.com/questions/5751335/using-file-listfiles-with-filenameextensionfilter – rsudha

+0

你可能想读Java I/O课程:http://docs.oracle.com/javase/tutorial/essential/io/它解释了如何在查找部分完全实现您想要的功能:http://docs.oracle.com/javase /tutorial/essential/io/find.html – XSen

回答

0
File f = new File("your folder path here");// your folder path 
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 veriable 

     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(); 
    } 
} 
+0

这太棒了! 我只是不想替换一个文件,我想创建一个以.xml结尾的新文件 但这很好,tnx! –

+0

@shine这将创建一个扩展名为“.xml”的新文件。如果文件已经存在,则内容将被覆盖。那就是'BufferWriter'的本质。 –

-1

很多优秀的人已经把代码片段在网上的这个。 Here是一个

但是,我可以问,为什么不直接重命名文件? This链接显示howto。

+0

因为其中一个是jack文件(代码是similler到java) 而另一个是xml文件。 我需要使用它们两个。 –

+0

所以你想从jack文件读取内容,创建xml内容,然后将其写入xml文件? – Hirak

+0

这正是我想要做的! –

0

列出文件夹中的所有 '.jack' 文件:

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

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

更换加长:

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

创建一个新的文件,并写入它:

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

把所有在一起:

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(); 
    } 
} 
相关问题