java基础---->java输入输出流
今天我们总结一下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输入输出流的更多相关文章
- 第27章 java I/O输入输出流
java I/O输入输出流 1.编码问题 import java.io.UnsupportedEncodingException; /** * java涉及的编码 */ public class En ...
- Java复习7.输入输出流
Java复习7.输入输出流 20131005 前言: Java中涉及数据的读写,都是基于流的,这一块的知识相当重要,而且在Java中的数据,char字符是16bit的,所以存在字节流和字符流的区别.如 ...
- Java基础-Java中23种设计模式之常用的设计模式
Java基础-Java中23种设计模式之常用的设计模式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.设计模式分类 设计模式是针对特定场景给出的专家级的解决方案.总的来说设 ...
- java基础---->java中正则表达式二
跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...
- Java基础-Java中的堆内存和离堆内存机制
Java基础-Java中的堆内存和离堆内存机制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- Java基础-Java中的内存分配与回收机制
Java基础-Java中的内存分配与回收机制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一. 二.
- Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock)
Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在学习Java的之前,你可能已经听说过读 ...
- Java基础-Java中的并法库之线程池技术
Java基础-Java中的并法库之线程池技术 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是线程池技术 二.
- Java基础-JAVA中常见的数据结构介绍
Java基础-JAVA中常见的数据结构介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是数据结构 答:数据结构是指数据存储的组织方式.大致上分为线性表.栈(Stack) ...
- Java基础-Java数据类型
Java基础-Java数据类型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据类型的作用 数据类型就是一组值,以及这一组值上的操作,数据类型可以决定数据的存储方式,取值范围 ...
随机推荐
- sqlserver日期函数<转>
一.sql server日期时间函数Sql Server中的日期与时间函数 1. 当前系统日期.时间 select getdate() 2. dateadd 在向指定日期加上一段时间的基 ...
- 让PHP7达到最高性能的几个Tips
PHP7 已经发布了,作为PHP十年来最大的版本升级,最大的性能升级,PHP7在多放的测试中都表现出很明显的性能提升,然而,为了让它能发挥出最大的性能,我还是有几件事想提醒下. PHP7 VS PHP ...
- JavaScript 关键字快速匹配
来源: http://www.cnblogs.com/index-html/archive/2013/04/17/js_keyword_match.html http://www.etherdream ...
- [从jQuery看JavaScript]-JavaScript
什么是JavaScript?相信随便百度Google一下都能找到一大堆的定义解释.而在我的理解中,JavaScript就是一种客户端的脚本语言,用于处理页面数据逻辑和用户体验(网页特效).实际上,Ja ...
- 数据库 Oracle数据库性能优化
--在Oacle数据库涉及到全表扫描的SQL查询(top,count)中, --现场用户删除表中大部分数据,只保留1W条数据,但是查询仍然很慢,检查磁盘IO,发现磁盘IO不是很高 --经过分析Oacl ...
- Hbase导入MapReduce数据的时候提示Running Job XXXX后就一直卡着不动
代码确信无误之后,ant运行起来,发现一执行就卡在Running Job XXXX那里一直不动了. 试着把代码打包成jar扔到Linux执行也还是一样的效果.还是停在那里.然后就一顿瞎蒙.最后发现是H ...
- C++ GDI+调用
封装了一个GDI+的使用类 GdiPluss.h #pragma once #include <comdef.h> #include <gdiplus.h> using nam ...
- nodejs基础 -- EventEmitter
var events = require('events'); nodejs所有的异步I/O操作在完成时都会发送一个事件到事件队列 nodejs里面的许多对象都会分发事件,如: 一个net.Serve ...
- Linux基础回想(1)——Linux系统概述
1. 什么是操作系统?它与硬件以及其它软件之间的关系是如何的? 操作系统是控制和管理计算机系统内各种硬件和软件资源.有效组织多道程序执行的系统软件(或程序集合),是用户和计算机之间的接口.详细的说: ...
- cake build使用:
开源地址: https://github.com/cake-build/cake 依赖 powershell 3.0 Windows 获取引导程序: Invoke-WebRequest http:// ...