码农编程阁-全国最大的中文编程交流平台

 找回密码
 立即注册
查看: 113|回复: 0

[文字教程] Java读写文件例子

[复制链接]
  • TA的每日心情
    擦汗
    2025-3-10 11:56
  • 签到天数: 95 天

    连续签到: 1 天

    [LV.6]常住居民II

    170

    主题

    902

    帖子

    2万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    26745
    发表于 2025-1-22 21:36:37 | 显示全部楼层 |阅读模式
    文件读取:
    [Java] 纯文本查看 复制代码
    import java.io.FileInputStream;
    import java.io.IOException;
    
    /**
     * 读取文件例子
     */
    public class FileInputDemo {
        public static void main(String[] args) throws IOException {
            System.out.println("demo");
            FileInputStream fis = new FileInputStream("test.txt");
            //读取test.txt内容并输出
            int len;
            byte[] buffer = new byte[1024];
            while ((len = fis.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, len));
            }
            fis.close();
            System.out.println("ok");
        }
    }
    

    写出文件:
    [Java] 纯文本查看 复制代码
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * 写出文件例子
     */
    public class FileOutputDemo {
        public static void main(String[] args) throws IOException {
            System.out.println("demo");
            FileOutputStream fos = new FileOutputStream("test.txt");
            String str = "hello 世界";
            fos.write(str.getBytes());
            fos.close();
            System.out.println("ok");
        }
    }
    

    读入文本文件内容:
    [Java] 纯文本查看 复制代码
    import java.io.FileReader;
    import java.io.IOException;
    
    /**
     * 读取文本文件内容
     */
    public class FileReaderDemo {
        public static void main(String[] args) throws IOException {
            FileReader fr = new FileReader("test.txt");
            int len = 0;
            //读取文本文件内容
            char[] buf = new char[1024];
            while ((len = fr.read(buf)) != -1) {
                //输出
                System.out.print(new String(buf, 0, len));
            }
            fr.close();
            System.out.println("ok");
        }
    }
    

    写出文本文件内容:
    [Java] 纯文本查看 复制代码
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * 写出文本文件内容
     */
    public class FileWriterDemo {
        public static void main(String[] args) throws IOException {
            FileWriter fw = new FileWriter("test.txt");
            //先清空原有内容在写入
            fw.write("你好世界,翻译为英文是 Hello World");
            //换行
            fw.write("\r\n");
            //获取当前时间
            String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            //继续追加内容
            fw.write(format);
            fw.close();
            System.out.println("ok");
        }
    }
    

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|码农编程阁-全国最大的中文编程交流平台

    GMT+8, 2025-4-29 21:32 , Processed in 0.133670 second(s), 23 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表