今天我们总结一下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.654
3.14156
我爱你!
23.654
2.366
我爱你!
*/

友情链接

java基础---->java输入输出流的更多相关文章

  1. 第27章 java I/O输入输出流

    java I/O输入输出流 1.编码问题 import java.io.UnsupportedEncodingException; /** * java涉及的编码 */ public class En ...

  2. Java复习7.输入输出流

    Java复习7.输入输出流 20131005 前言: Java中涉及数据的读写,都是基于流的,这一块的知识相当重要,而且在Java中的数据,char字符是16bit的,所以存在字节流和字符流的区别.如 ...

  3. Java基础-Java中23种设计模式之常用的设计模式

    Java基础-Java中23种设计模式之常用的设计模式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.设计模式分类 设计模式是针对特定场景给出的专家级的解决方案.总的来说设 ...

  4. java基础---->java中正则表达式二

    跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...

  5. Java基础-Java中的堆内存和离堆内存机制

    Java基础-Java中的堆内存和离堆内存机制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  6. Java基础-Java中的内存分配与回收机制

    Java基础-Java中的内存分配与回收机制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一. 二.

  7. Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock)

    Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在学习Java的之前,你可能已经听说过读 ...

  8. Java基础-Java中的并法库之线程池技术

    Java基础-Java中的并法库之线程池技术 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是线程池技术 二.

  9. Java基础-JAVA中常见的数据结构介绍

    Java基础-JAVA中常见的数据结构介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是数据结构 答:数据结构是指数据存储的组织方式.大致上分为线性表.栈(Stack) ...

  10. Java基础-Java数据类型

    Java基础-Java数据类型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据类型的作用 数据类型就是一组值,以及这一组值上的操作,数据类型可以决定数据的存储方式,取值范围 ...

随机推荐

  1. win8.1 win10存储设备和驱动器分开显示

    win10同理如下: Windows 8.1不仅将资源管理器重命名为文件管理器,还将用户熟悉的“计算机/我的电脑”改名为“这台电脑”,同时还将原先的布局进行了重构,于是用户最终看到的是这样一个界面: ...

  2. Linux启动与禁止SSH用户及IP的登录

    以下就针对SSH方面讨论一下.假设有人特别关注Linux环境的安全性,第一就从login方面来进行讨论 1:Linux启动或禁止SSH root用户的登录 2:Linux限制SSH用户 事实上这些东西 ...

  3. Xcode使用介绍

    ///// 应用程序文件的组织 Product Name:项目名字 Organization Name:组织机构名称 Company Identifier:公司唯一标识符 Bundle Identif ...

  4. Android Studio:Multiple dex files define Landroid/support/annotation/AnimRes

    近期真的比較忙,一不小心博客又荒了两个月. 从今天起.决定重返csdn,多多纪录和分享. 先从一个近期被折磨的死去活来的问题. 由于升级了V4包,就一直报这个问题: com.android.dex.D ...

  5. WPF教程五:布局之Canvas面板

    Canvas:画布面板 画布,用于完全控制每个元素的精确位置.他是布局控件中最为简单的一种,直接将元素放到指定位置,主要来布置图面.使用Canvas,必须指定一个子元素的位置(相对于画布),否则所有元 ...

  6. C++ GDI+调用

    封装了一个GDI+的使用类 GdiPluss.h #pragma once #include <comdef.h> #include <gdiplus.h> using nam ...

  7. Understanding the difficulty of training deep feedforward neural networks

    本文作者为:Xavier Glorot与Yoshua Bengio. 本文干了点什么呢? 第一步:探索了不同的激活函数对网络的影响(包括:sigmoid函数,双曲正切函数和softsign y = x ...

  8. ubuntu 访问 共享 windows文件夹

    sudo mount -o username=*******,password=******** //192.168.1.105/迅雷下载 /mnt/

  9. 在GIT中创建一个空分支

    ref:  https://segmentfault.com/a/1190000004931751

  10. CentOS下 Uptime 命令

    对于一些人来说系统运行了多久是无关紧要的,但是对于服务器管理员来说,这是相当重要的信息.服务器在运行重要应用的时候,必须尽量保证长时间的稳定运行,有时候甚至要求零宕机.那么我们怎么才能知道服务器运行了 ...