try{
FileOutputStream fos = new FileOutputStream("e.txt");
String s="fjkdsl";
fos.write(s.getBytes());
fos.close();
}catch(IOException e){
}

解决方案 »

  1.   

    如果用BufferedWriter怎么实现?
      

  2.   

    一样可以用write方法写文件即可实现!
      

  3.   

    try{
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("e.txt")));
    String s="fjkdsl";
    out.write(s,0,s.length());
    out.close();
    }catch(IOException e){
    }
      

  4.   

    我的一个小例子。jdk1.4+win2k下调试成功。import java.io.FileOutputStream;
    import java.io.IOException;
    public class StrToFile
    {
    public static void main(String[] args)
    {
    String str="This is a string.";
    try
    {
    FileOutputStream fout=new FileOutputStream("strtofile.txt");
    fout.write(str.getBytes());
    fout.close();
    }
    catch(IOException e){System.out.println(e.getMessage());}
    }
    }
      

  5.   

    PrintWriter和BufferedWriter都是继承java.io.Writer,所以很多功能都一样。不过PrintWriter提供println()方法可以写不同平台的换行符,而BufferedWriter可以任意设定缓冲大小。
    OutputStream可以直接传给PrintWriter(BufferedWriter不能接收),如:
    PrintWriter out
       = new PrintWriter(new BufferedOutputStream(new FileOutputStream("foo.out")));
    或者用OutputStreamWriter来将OutputStream转化为Wrtier.这时就可以用BufferedWriter了。
    API documents中写的很清楚了。
      

  6.   

    有两条字串
    s=addf,fdfd,fdsfdf
    b=ddff,fdfddf,weewew
    我想把他们写到文件qq.txt中去,串s在第一行,串b在第二行,
    但是会出现串b把串c覆盖了的问题
    请问怎么不覆盖的把他们按上述要求考到文件中去呢?
      

  7.   

    好像有一个参数可以控制覆盖还是append,你只要在parameter list里边注明就可以了。至于那个参数我有点忘了。自己看一下API