日期:2014-05-20  浏览次数:20640 次

IO 怎么把String里的内容写入到file文件里
麻烦请写下 示例 代码

------解决方案--------------------

// 随便网上一找都有的..
public class JavaFileIO {

    public static void main(String[] args) {
        try {

          String content = "this is text content..";
          File file = new File("myfile2.txt");
          
         if(!file.exists()){
             file.createNewFile();
         }
         FileWriter fw = new FileWriter(file.getName());
         BufferedWriter bw = new BufferedWriter(fw);
         bw.write(content);
         bw.close();
          
          System.out.println("Done.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

------解决方案--------------------
最简单那的一个例子
String result;//要写入的内容
String file;//要写入的文件路径
public static void writeInTxt(String result,String file) throws Exception {
// TODO Auto-generated method stub
ifexist(file);
PrintWriter pw=new PrintWriter(new FileWriter(file));
pw.print(result);
pw.flush();
System.out.println("写入成功");

}

------解决方案--------------------
文件后缀为。html不就行了。你又不是web项目,没用到http请求,要是用到的话,直接PrintWriter.println()  
------解决方案--------------------
要写成html的用DOM4j或JDOM的xml方式去写要好