输入输出流

复制代码
package LIU;

import java.io.*;
public class TestFile3 {

    public static void main(String[] args) {
        //字节写入读出
        
        
        try{
            //写入
            FileOutputStream fos=new FileOutputStream("d:\\test1.txt",true);
            
            //String str2="追加写入";
            String str="\n大家下午好 ";
            fos.write(str.getBytes());//覆盖写入
            
            //追加写入
            //FileOutputStream(name,true)
            
            
            fos.close();
            //读出
            FileInputStream fis=new FileInputStream("d:\\test1.txt");
            byte[]b=new byte[200];
            int i=fis.read(b);
            String str1=new String(b,0,i);
            System.out.println("读取内容:"+str1);
            fis.close();
            
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        


复制代码
package LIU;


import java.io.*;
public class TsetFile4 {

    public static void main(String[] args) {
        
        try {
            //读取
            FileReader fr=new FileReader("d:\\test1.txt");
            char[]c=new char[200];
            
            int i=fr.read(c);
            String str=new String(c,0,i);
            System.out.println("读取内容:"+str);
            fr.close();
            //写入
            FileWriter fw=new FileWriter("d:\\test1.txt",true);
            fw.write("\n新写入的内容");
            fw.close();
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
复制代码

 


复制代码

 

posted @   百事没事  阅读(182)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示