今天我们总结一下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. C#操作word类文件

    最近频繁操作Word文档,写了很多word的操作代码及方法,虽然已经有很多关于word的操作类了,自己还是进行了一下整合: 1.通过模板创建新文件 2.在书签处插入值 3.插入表格 4.合并单元格 5 ...

  2. 第一关练习题统计网站最大访问量sed法,隐藏知识数组下标不能重复

    1.1.1 获取日志的最大top10,排序 获取两列到新的文件中第一次处理 sed截取字符串中间的内容,sed不支持贪婪匹配.找出图片在的列和图片大小到test1文件 本题需要输出三个指标:[访问次数 ...

  3. 上手并过渡到PHP7(2)——必须传递int, string, bool参数?没问题

    Type hints, Type safe 泊学实操视频 泊学原文链接PHP 7中最引人注目的新特性之一,无疑是Scalar type hints.我们可以在函数参数和返回值中使用scalar typ ...

  4. 关于Cocos2d-x中字体的使用

    1.如果使用的是系统自带的 static Label* createWithSystemFont ( const std::string & text,        const std::s ...

  5. ubuntu配置apache和cgi

    ubuntu配置apache和cgi . 更新源并进行安装,否则后面的下载可能会不成功. sudo apt-get update sudo apt-get upgrade . 安装apache2服务 ...

  6. vs2008 x64编译环境 忽略了 #ifdef WIN32

    解决方法: 右键项目属性,在预处理器中添加WIN32即可 效果:

  7. e621. Activating a Keystroke When Any Child Component Has Focus

    Normally, a keystroke registered on a component is activated when the component has the focus. This ...

  8. 奇葩问题:ListView中Item与Item中的Button不能单击问题

    android中ListView是一个经常要用到的一个组件,用到该组件时经常会碰到ListView的Item和Item中的Button不能单击的问题. 本人在使用时同样也遇到过这样的情况,共有三种情况 ...

  9. (38)JS运动之淡入淡出

    基本思路:使用样式filter.可是要区分IE浏览器和chrom.firefox的不同,详细也是用定时器来实现. <!DOCTYPE HTML> <!-- --> <ht ...

  10. Sublime Text3打造U盘便携Lua IDE

    下载Sublime Text  链接http://www.sublimetext.com/3 我下载的是win32 portable 版 便于放入U盘携带 解压 注冊: 能够复制下面部分直接贴入注冊栏 ...