FileWriter向文件中写入内容

1.创建fileWriter对象

2.写入文件信息

3.关闭流

必须关闭流,否则将不能写入文件中

/**
 * 
 * @author Administrator
 *    文件写入对象的使用
 *
 */
public class fileWriterTest {
    
    public static void main(String[] args) {
         File file1 = new File("d:\\test.txt");
            FileWriter fw;
            try {
                fw = new FileWriter(file1);
                 fw.write(97);
                    fw.write("this is 我们");
                    fw.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
           
        
        try {
        //1.创建文件
        File desFile = new File("aa\\bb");
        
        File sourceFile = new File(desFile,"aa.txt");
        
        //2.创建文件写入对象
        FileWriter writer = new FileWriter(sourceFile);
        
        //3.写入整数
        writer.write(1);
    
        writer.close();
        
         FileReader fr = new FileReader(sourceFile);
        System.out.println( fr.read());
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

转载于:https://www.cnblogs.com/itcx1213/p/8023842.html

你可能感兴趣的:(FileWriter向文件中写入内容)