保存String到文本文件怎么写给个例子,最好帮我写一个选择要保存文件地址的例子

解决方案 »

  1.   

    使用JFileChooser,选取文件。
    例子可以在 http://www.java2s.com/Tutorial/Java/0240__Swing/1260__JFileChooser.htm
      

  2.   

    String str = "所要保存的字符串";
    char [] c = new char[str.length()];
    for(int i=0;i<str.length();i++)
    {
         c[i] = str.charAt(i);
    }
    FileWriter fw = null;
    try{
         fw = new FileWriter("文件地址");
         fw.write(c);
    }
    catch(IOException e)
    {
       System.out.println(e.getMessgae());
       e.printStackTrace();
    }
    finally{
       try{
       if(fw != null){fw.close();}
      }
       catch(IOException e)
       {
            e.printStrackTrace();
       }
    }
      

  3.   


    package org.apache.struts2.showcase.ajax;import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    FileWriter fw = null;
    File f = new File("aaaa.txt");
    try {
    if (!f.exists()) { f.createNewFile();
    }
     fw = new FileWriter(f);
    fw.write("aaaaaaaaaaaaaaaaaaaaaaa");
    fw.flush();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    fw.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }