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

请问我如何使用file对象来向文件中输入内容?
我要在Hello.txt中输入内容应该怎么做呢?

public class Ex6_2 {
  public Ex6_2()
  {
  }
  public static void main(String arg[])throws IOException{
  File f = new File("D:" + File.separator + "Hello.txt");
  f.createNewFile();
  FileInputStream in=new FileInputStream("Hello.txt");
  }

------解决方案--------------------
Java code

import java.io.*; 
public class Ex6_2 { 
   
        public static void main(String arg[])throws IOException{ 
        File f = new File("D:" + File.separator + "Hello.txt"); 
        f.createNewFile(); 
        s1 += "要添加的内容";
        BufferedWriter output = new BufferedWriter(new FileWriter(f)); 
        output.write(s1); 
        output.close(); 
           }

------解决方案--------------------
write
------解决方案--------------------
S1应该是String类型的变量,1楼可能忘了在S1前加String修饰
------解决方案--------------------
String str=new String("自己要输入的内容");
File file = new File("D:\Hello.txt"); 
FileWriter writer=new FileWriter(file);
writer.write(str);
writer.close() ;
------解决方案--------------------
File file=new File("c:/mulu.txt");
FileWriter filewriter=new FileWriter(file);
String s=new String();
filewrite.write(s);
filewrite.close();

------解决方案--------------------
File对象不能写文件的,必须用流才可以写入,如FileWriter或FileOutputStream