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数据类型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据类型的作用 数据类型就是一组值,以及这一组值上的操作,数据类型可以决定数据的存储方式,取值范围 ...
随机推荐
- 【WPF】样式与模板:鼠标移入/悬浮时按钮的背景色不改变
情况:鼠标移到按钮上,默认情况是按钮背景色会改变的,网上也能搜到很多如何自定义改变的背景色. 需求:现在需求反过来,想要鼠标移到按钮上,保持按钮的背景色不改变. 一种思路:在样式文件中,使用Multi ...
- Elastic-Job - 分布式定时任务框架
Elastic-Job - 分布式定时任务框架 摘要 Elastic-Job是ddframe中dd-job的作业模块中分离出来的分布式弹性作业框架.去掉了和dd-job中的监控和ddframe接入规范 ...
- Hive Tuning(四) 从查询计划看hive.auto.convert.join的好处
今天我们来讲一下如何看懂Hive的查询计划. hive的执行计划包括三部分 – Abstract syntax tree – 可以直接忽略 – Stage dependencies – 依赖 – S ...
- c#生成rsa公钥和私钥
c#生成rsa公钥和私钥的类库,包括加密解密,可以用在网站和winform项目 源码地址: http://download.csdn.net/detail/jine515073/8383809
- ps -a,job,netstat,daemons
kill -9是强制 -15是正常后执行 进程中的数据都写到 /proc/*这个目录下 进程 ps -l aux -lA zxjf(进程树) job ...
- android之存储篇_SQLite数据库入门
SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么. 例如:可以在Integer类型的字段中存放字符串,或者在布尔型字段中存放浮点数,或者在字符型字段中 ...
- 页面装载js及性能分析方法
一.装载 先装载静态页面的引用js文件,然后查找引用文件中是否包含onload函数,比如main.js中包含onload函数,在main.js中查找是否有对其他js文件的引用,优先装载引用js文件,被 ...
- 各个层次的gcc警告
http://blog.csdn.net/lizzywu/article/details/9419145 各个层次的gcc警告从上到下覆盖 变量(代码)级:指定某个变量警告 int a __attri ...
- XML 入门
XML语法 所有 XML 元素都须有关闭标签 XML 标签对大小写敏感 XML 必须正确地嵌套 XML 文档必须有根元素 就像HTML一样,HTML必须有<html>根元素.XML也必须有 ...
- 在Terminal中的光标的使用技巧
如何简单操作? 在 Terminal(终端) 中,有许多操作技巧,这里就介绍几个简单的. 光标 up(方向键上) 可以调出输入历史执行记录,快速执行命令 down(方向键下) 配合 up 选择历史执行 ...