• java 实现文件读写操作


     1 import java.io.FileReader;
     2 import java.io.FileWriter;
     3 import java.io.IOException;
     4 /*  JAVA IO 读写操作  2019年11月29日17:28:27 */
     5 public class test {
     6     public static void main(String[] args) throws IOException {
     7        //调用读的方法
     8         readDiskFileText();
     9     }
    10     //读取硬盘文件
    11     public static void readDiskFileText() throws IOException {
    12         //建立要读取的文件对象
    13         FileReader fr = new FileReader("H:/(2).txt");
    14         //存储读取到的多个字符
    15         char[] cs = new char[1024];
    16         //记录的是每次读取的有效字符个数
    17         int len = 0;
    18         //读取的字符数
    19         String kk="";
    20         while((len = fr.read(cs))!=-1){
    21             //把一个字节数组by从0取到len,取出来之后转换成String类型
    22             String i5=new String(cs,0,len);
    23             kk+=i5;
    24         }
    25         //读取的同时进行写入
    26         f2(kk);
    27         //3.释放资源
    28         fr.close();
    29     }
    30 
    31     //向写入文件硬盘      写入文件例子
    32     public static void fi() throws IOException {
    33         FileWriter fw = new FileWriter("123.txt");
    34         fw.write("2212");
    35         fw.write('
    ');
    36         fw.write("123");
    37         fw.close();
    38         System.out.println("11111");
    39     }
    40 
    41     //文件复制
    42     public static void f2(String ii) throws IOException {
    43         FileWriter fw = new FileWriter("1.txt");
    44         fw.write(ii);
    45         fw.close();
    46         System.out.println("文件写入完成");
    47     }
    48 }
    java 实现文件的读写操作 复制文件内容
  • 相关阅读:
    VKD224B触摸芯片调试笔记
    liunx 常用命令学习笔记
    2440 裸机学习 点亮LED
    单端正激变换器
    c# 文件与流
    c# 接口笔记
    Ubuntu18.04 server安装步骤
    how to force git to overwritten local files
    Linux基础
    解决Linux下Firefox无法启动的问题
  • 原文地址:https://www.cnblogs.com/zhenqk/p/11971785.html
Copyright © 2020-2023  润新知