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数据类型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据类型的作用 数据类型就是一组值,以及这一组值上的操作,数据类型可以决定数据的存储方式,取值范围 ...
随机推荐
- Java项目中如何扩展第三方jar包中的类?
有些时候你对第三方得到jar包中的类并不是很满意,想根据实际情况做一些扩展.如果说第三方的jar包已经提供了一些可扩展的类,比如提供了Interceptor,Filter或者其他的类,那么使用原生的比 ...
- C语言 · 最大子阵
历届试题 最大子阵 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个n*m的矩阵A,求A中的一个非空子矩阵,使这个子矩阵中的元素和最大. 其中,A的子矩阵指在A中行和 ...
- size_t ssize_t socklen_t
size_t 解释一:为了增强程序的可移植性,便有了size_t,它是为了方便系统之间的移植而定义的,不同的系统上,定义size_t可能不一样. 在32位系统上 定义为 unsigned int 也就 ...
- pthread_cond_wait()函数的详解
http://hi.baidu.com/tjuer/item/253cc6d66b921317d90e4483 了解 pthread_cond_wait() 的作用非常重要 -- 它是 POSIX 线 ...
- 測试Service
<strong><span style="font-size:18px;">自己定义Service:</span></strong> ...
- 从实例中学习grid布局
对于Web开发者来说,网页布局一直是个比较重要的问题. Web 布局主要经历了以下四个阶段: 1.table表格布局: 2.float浮动及position定位布局: 3.flex弹性盒模型布局,革命 ...
- Jquery 技术小结
前记: 现在项目中经常要用到JS去操作一些事,对整个团队开发来说,JS的书写规范和正确对开发具有较大的帮助.在一个团队中常常会发生JS书写的不统一性和游览器不兼容性等情况发生.我觉的最好的方法就是有一 ...
- 移动H5功能设计反思 测试用例总结
一.线上页面滑动流畅性测试 1.减少长动画效果(影响流畅) 2.是否自动跳转或者还是让用户自己操作跳转需要推敲 二.buttom和页面滑动的选择(优劣) 部分手机本身就会滑动不灵敏,大部分时候其实用b ...
- android 监听Home键
/** * Home 键监听,当按下Home键时,系统会发出action为Intent.ACTION_CLOSE_SYSTEM_DIALOGS的BroadcastReceiver * 在程序里动态注册 ...
- spring只是一个框架
想跟着 spring in action 4 系统的研究下spring,结果发现忘了怎么建一个spring项目. 关键是,不知道该建一个什么项目,Java项目?Maven项目(Java项目?Web项目 ...