javaOOP 读取硬盘上的一个文本文件

    xiaoxiao2021-04-19  82

    package cn.happy2; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Day01 { /** * 1.1 读取硬盘上的一个文本文件 * @throws IOException */ public static void main(String[] args) throws IOException { //FileInputStream FileInputStream fis=new FileInputStream("E:/S2226.txt"); //1.定义一个缓冲区 byte[] byte[] bytes=new byte[1024];//1k 1.5k // int data; while((data=fis.read(bytes))!=-1){ //有数据可读 //byte[] String String temp=new String(bytes,0,data); System.out.println(temp); } fis.close(); } } 写入文件 package cn.happy2; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Day02 { /** * 1.1 写入文件 * * @throws IOException * * */ public static void main(String[] args) throws IOException { String words="写入)"; FileOutputStream fos=new FileOutputStream("E:/S2226.txt"); byte[] bytes = words.getBytes(); fos.write(bytes); fos.close(); System.out.println("ok!"); } }
    转载请注明原文地址: https://ju.6miu.com/read-675763.html

    最新回复(0)