博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA基础——常用IO使用
阅读量:4044 次
发布时间:2019-05-24

本文共 3435 字,大约阅读时间需要 11 分钟。

 常用的文件流:FileInputStream/FileOutputStream, FileReader/FileWriter

package com.lzz.IO;import java.io.FileInputStream;import java.io.IOException;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;public class TestIO {    public static void FileInputStreamTest() throws IOException {        FileInputStream fis = new FileInputStream("E:/笔记.txt");        byte[] buf = new byte[1024];        int hasRead = 0;        //read()返回的是单个字节数据(字节数据可以直接专程int类型),但是read(buf)返回的是读取到的字节数,真正的数据保存在buf中        while ((hasRead = fis.read(buf)) > 0) {            //每次最多将1024个字节转换成字符串,这里tmp2.txt中的字符小于1024,所以一次就读完了            //循环次数 = 文件字符数 除以 buf长度            System.out.println(new String(buf, 0 ,hasRead));            /*             * 将字节强制转换成字符后逐个输出,能实现和上面一样的效果。但是如果源文件是中文的话可能会乱码            for (byte b : buf)    {                char ch = (char)b;                if (ch != '\r')                System.out.print(ch);            }            */        }        //在finally块里close更安全        fis.close();    }    public static void FileReaderTest() throws IOException {        try (                // 在try() 中打开的文件, JVM会自动关闭                FileReader fr = new FileReader("E:/笔记.txt")) {            char[] buf = new char[32];            int hasRead = 0;            // 每个char都占两个字节,每个字符或者汉字都是占2个字节,因此无论buf长度为多少,总是能读取中文字符长度的整数倍,不会乱码            while ((hasRead = fr.read(buf)) > 0) {                // 如果buf的长度大于文件每行的长度,就可以完整输出每行,否则会断行。                // 循环次数 = 文件字符数 除以 buf长度                System.out.println(new String(buf, 0, hasRead));                // 跟上面效果一样                // System.out.println(buf);            }        } catch (IOException ex) {            ex.printStackTrace();        }    }    public static void FileOutputStreamTest() throws FileNotFoundException, IOException {        try (                //在try()中打开文件会在结尾自动关闭                FileInputStream fis = new FileInputStream("E:/笔记.txt");                FileOutputStream fos = new FileOutputStream("E:/笔记1.txt");        ) {            byte[] buf = new byte[4];            int hasRead = 0;            while ((hasRead = fis.read(buf)) > 0) {                //每读取一次就写一次,读多少就写多少                fos.write(buf, 0, hasRead);            }            System.out.println("write success");        } catch (IOException e) {            e.printStackTrace();        }    }    public static void FileWriterTest() throws IOException {        try (FileWriter fw = new FileWriter("E:/笔记1.txt")) {            fw.write("天王盖地虎\r\n");            fw.write("宝塔镇河妖\r\n");        } catch (IOException e) {            e.printStackTrace();        }    }    public static void main(String[] args) throws IOException {        //FileInputStreamTest();        //FileReaderTest();        //FileOutputStreamTest();        FileWriterTest();    }}

Java中读取某个目录下的所有文件和文件夹

package com.lzz.IO;import com.amazonaws.services.dynamodbv2.xspec.S;import java.io.File;public class Test1 {    public static void main(String[] args) {          String path="E:/";          File file=new File(path);          File[] tempList = file.listFiles();          System.out.println("该目录下对象个数:"+tempList.length);          for (int i = 0; i < tempList.length; i++) {          if (tempList[i].isFile()) {             System.out.println("文件:"+tempList[i]);          }         if (tempList[i].isDirectory()) {              System.out.println("文件夹:"+tempList[i]);         }      }    }}

 

转载地址:http://hqwci.baihongyu.com/

你可能感兴趣的文章
android 代码实现圆角
查看>>
flutter-解析json
查看>>
android中shader的使用
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>