博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java基础---->java输入输出流
阅读量:4593 次
发布时间:2019-06-09

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

  今天我们总结一下java中关于输入流和输出流的知识,博客的代码选自Thinking in java一书。我突然很想忘了你,就像从未遇见你。

 

java中的输入流

huhx.txt文件的内容如下: I love you, ch. 中文

一、缓冲中输入文件

public class BufferedInputFile {    public static String read(String filename) {        BufferedReader reader = null;        StringBuilder builder = new StringBuilder();        try {            reader = new BufferedReader(new FileReader(filename));            String line;            while ((line = reader.readLine()) != null) {                builder.append(line + "\n");            }        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                reader.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return builder.toString();    }    public static void main(String[] args) {        String content = read("file/huhx.txt");        System.out.println(content);    }}

 

二、从内存输入

public class MemoryInput {    public static void main(String[] args) {        StringReader reader = new StringReader(BufferedInputFile.read("file/huhx.txt"));        int c;        try {            while ((c = reader.read()) != -1) {                System.out.print((char) c);            }        } catch (IOException e) {            e.printStackTrace();        }    }}

 

三、格式化的内存输入

public class FormattedMemoryInput {    public static void main(String[] args) {        DataInputStream inputStream = new DataInputStream(                new ByteArrayInputStream(BufferedInputFile.read("file/huhx.txt").getBytes()));        try {            while (inputStream.available() != 0) {                System.out.print((char)inputStream.readByte());            }        } catch (IOException e) {            e.printStackTrace();        }    }}

available()的工作方式会随着所读取的媒介类型的不同而有所不同。也就是说在没有阻塞的情况下所能读取的字节数。对于文件,这意味着整个文件。但是对于不同类型的流,可能就不是这样的。

 

java中输出流

一、基本的文件输出

public class BasicFileOutput {    static String file = "file/linux.txt";    public static void main(String[] args) {        BufferedReader reader = new BufferedReader(new StringReader(BufferedInputFile.read("file/huhx.txt")));        PrintWriter out = null;        try {            out = new PrintWriter(new BufferedWriter(new FileWriter(file)));            String line;            while ((line = reader.readLine()) != null) {                out.println(line);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            out.close();        }        System.out.println(BufferedInputFile.read(file));    }}

 

二、文本文件输出的快捷方式

public class FileOutputShortcut {    static String file = "file/linux.out"; // 这个也和上述不一样    public static void main(String[] args) {        BufferedReader reader = new BufferedReader(new StringReader(BufferedInputFile.read("file/huhx.txt")));        PrintWriter out = null;        try {            out = new PrintWriter(new FileWriter(file)); // 这里和上述不一样            String line;            while ((line = reader.readLine()) != null) {                out.println(line);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            out.close();        }        System.out.println(BufferedInputFile.read(file));    }}

 

三、存储和恢复数据

public class StoringAndRecoveringData {    public static void main(String[] args) {        try {            DataOutputStream out = new DataOutputStream(                    new BufferedOutputStream(new FileOutputStream("file/chenhui.txt")));            out.writeDouble(3.1415926);            out.writeUTF("中文可以吗?");            out.writeInt(123);            out.close();            DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("file/chenhui.txt")));            System.out.println(in.readDouble()); // 如果这里是readInt(),会导致后面的readUTF方法报错            System.out.println(in.readUTF());            System.out.println(in.readInt());            in.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

 

三、读写随机访问文件

public class UsingRandomAccessFile {    static String file = "file/huhx.txt";    static void display() throws Exception {        RandomAccessFile rf = new RandomAccessFile(file, "r");        System.out.println(rf.readDouble());        System.out.println(rf.readDouble());        System.out.println(rf.readUTF());        rf.close();    }    public static void main(String[] args) {        RandomAccessFile rf;        try {            rf = new RandomAccessFile(file, "rw");            rf.writeDouble(23.654);            rf.writeDouble(3.14156);            rf.writeUTF("我爱你!");            rf.close();            display();            rf = new RandomAccessFile(file, "rw");            rf.seek(1 * 8);             rf.writeDouble(2.366);            rf.close();            display();        } catch (Exception e) {            e.printStackTrace();        }    }}/*23.6543.14156我爱你!23.6542.366我爱你!*/

 

友情链接

 

转载于:https://www.cnblogs.com/huhx/p/baseusejavafile1.html

你可能感兴趣的文章
月薪20K的程序员整理的C语言的学习笔记,值得学习!
查看>>
Swing应用开发实战系列之二:设计日期选择面板窗口
查看>>
Swing应用开发实战系列之一:自定义JdbcTemplate
查看>>
Java随笔一:String类中方法split
查看>>
(转)使用LVS实现负载均衡原理及安装配置详解
查看>>
01整数规划
查看>>
a recipe kindly provided by Dimas for kikuchi
查看>>
icon design隐私条款
查看>>
移动端开发
查看>>
3. Elements of a Test Plan
查看>>
通过NuGet获取sqlite对应的.net的dll
查看>>
用户和用户组,以及文件和文件夹的权限
查看>>
H5 基于Web Storage 的客户端留言板
查看>>
linux添加字体
查看>>
Fastjson是一个Java语言编写的高性能功能完善的JSON库。
查看>>
一篇和Redis有关的锁和事务的文章
查看>>
delphi验证手机号码地址的正则表达式验证function
查看>>
sublime 我的快捷键
查看>>
asp.net MVC日志插件Log4Net学习笔记一:保存日志到本地
查看>>
9-16Jenkins-1第一个任务
查看>>