代码实现:

import java.io.*;

public class StringDemo { public static void main(String[] args) { try { //循环随机输入三段字符串 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringBuffer stringBuffer = new StringBuffer(); for (int i = 1; i <= 3; i++) { System.out.println("请输入第" + i + "段字符串:"); String str = reader.readLine(); stringBuffer.append(str); } //将拼接后的字符串写入文件 FileOutputStream fileOutputStream = new FileOutputStream("c:\inputDemo.txt"); fileOutputStream.write(stringBuffer.toString().getBytes()); fileOutputStream.close(); System.out.println("文件写入成功!");

        //读取文件内容并打印
        FileInputStream fileInputStream = new FileInputStream("c:\\inputDemo.txt");
        byte[] bytes = new byte[fileInputStream.available()];
        fileInputStream.read(bytes);
        String content = new String(bytes);
        System.out.println("文件内容为:" + content);
        fileInputStream.close();

        //复制文件内容到另一个文件
        FileInputStream fileInputStream2 = new FileInputStream("c:\\inputDemo.txt");
        FileOutputStream fileOutputStream2 = new FileOutputStream("c:\\inputDemo2.txt");
        byte[] bytes2 = new byte[fileInputStream2.available()];
        fileInputStream2.read(bytes2);
        fileOutputStream2.write(bytes2);
        fileInputStream2.close();
        fileOutputStream2.close();
        System.out.println("文件复制成功!");

    } catch (IOException e) {
        e.printStackTrace();
    }
}
第三题 请用键盘循环随机输入三段字符串1:将三段字符串用Stringbuffer类拼接成一个字符串并将该字符串写入cinputDemotxt文件;写文件用字节流实现2:读取cinputDemotxt文件并打印文件内容读文件用字节流实现3:将cinputDemotxt文件中的内容复制到cinputDemo2txt文件中读写文件用字节流实现用java

原文地址: https://www.cveoy.top/t/topic/fNdH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录