Java IO API记录
文件路径:
public static final String FILEPATH= File.separator+"Users"+ File.separator+"xuminzhe"+
File.separator+"Documents"+File.separator+"io";
1.创建文件
public static void main(String[] args) {
File file=new File(Constant.FILEPATH+File.separator+"io.text");
try {
boolean newFile = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
2.查找指定目录下文件
public static void main(String[] args) {
File file=new File(Constant.FILEPATH);
File[] str = file.listFiles();
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}
3.文件流-写入
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
OutputStream outputStream=new FileOutputStream(file,true);
byte[] bytes = "你好".getBytes();
for (int i = 0; i < bytes.length; i++) {
outputStream.write(bytes[i]);
}
outputStream.close();
4.文件流-读取
public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
InputStream inputStream=new FileInputStream(file);
/**
* 单字节读取
*/
{
byte[] bytes = new byte[1024];
int read1;
int count =0;
while((read1 = inputStream.read())!=-1){
bytes[count++]=(byte) read1;
}
System.out.println(new String(bytes));
}
/**
* 多字节读取
*/
{
byte[] bytes=new byte[(int) file.length()];
int read;
while((read=inputStream.read(bytes))!=-1){
System.out.println(new String (bytes));
}
}
}
5.字符流-写入
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Writer writer=new FileWriter(file,true);
String str="\r\nhello";
writer.write(str);
writer.close();
6.字符流-读取
public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Reader read=new FileReader(file);
char[] ch=new char[100];
int temp=0;
int count=0;
while((temp=read.read())!=(-1)){
ch[count++]=(char)temp;
}
read.close();
System.out.println("内容为"+new String(ch,0,count));
}
7.转换流-写入 将输出的字符流转化为字节流
public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Writer writer=new java.io.OutputStreamWriter(new FileOutputStream(file,true));
writer.write("kobe");
writer.close();
}
8.转换流-读取 将输入的字节流转换为字符流
public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
Reader read = new InputStreamReader(new FileInputStream(file));
char[] b=new char[100];
int len=read.read(b);
System.out.println(new String(b,0,len));
read.close();
}
9.对象流
static String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
static File file=new File(filename);
public static void main(String[] args) throws Exception {
serializable(file);
deserializable(file);
}
/**
* 反序列化
* @param file
* @throws IOException
* @throws ClassNotFoundException
*/
private static void deserializable(File file) throws IOException, ClassNotFoundException {
ObjectInputStream stream=new ObjectInputStream(new FileInputStream(file));
Person o = (Person) stream.readObject();
System.out.println(o.toString());
} /**
* 序列化对象
* @param file
* @throws IOException
*/
private static void serializable(File file) throws IOException {
ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream(file,true));
outputStream.writeObject(new Person("xmz",13));
outputStream.close();
}
10.缓冲字符流-读取
public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
String line;
while((line=bufferedReader.readLine())!=null){//读取一个文本行
System.out.println(line);
}
bufferedReader.close();
}
11.缓冲字符流-写入
public static void main(String[] args) throws IOException {
String filename=Constant.FILEPATH+ File.separator+"HELLO.text";
File file=new File(filename);
FileWriter fileWriter=new FileWriter(file);
/**
* 为了提高写入的效率,使用了字符流的缓冲区。
* 创建了一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联。
*/
BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
bufferedWriter.write("jordan乔丹");
bufferedWriter.newLine();//换行
bufferedWriter.write("kobe蜗壳");
bufferedWriter.write("wade韦德");
bufferedWriter.flush();
bufferedWriter.close();
}
12 管道流-可用于线程通信
static class Send implements Runnable{
private PipedOutputStream out=null;
public Send() {
out=new PipedOutputStream();
}
public PipedOutputStream getOut(){
return this.out;
}
public void run(){
String message="hello,xmz";
try{
out.write(message.getBytes());
}catch (Exception e) {
e.printStackTrace();
}try{
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 接受消息类
* */
static class Recive implements Runnable{
private PipedInputStream input=null;
public Recive(){
this.input=new PipedInputStream();
}
public PipedInputStream getInput(){
return this.input;
}
public void run(){
byte[] b=new byte[1000];
int len=0;
try{
len=this.input.read(b);
}catch (Exception e) {
e.printStackTrace();
}try{
input.close();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("接受的内容为 "+(new String(b,0,len)));
}
}
public static void main(String[] args) throws IOException {
Send send=new Send();
Recive recive=new Recive();
try{
//管道连接
send.getOut().connect(recive.getInput());
}catch (Exception e) {
e.printStackTrace();
}
new Thread(send).start();
new Thread(recive).start();
}
Java IO API记录的更多相关文章
- java io学习记录(路径分隔符)
java路径分隔符(路径表示) path="E:\\xp\\test\\2.jpg"; path="E:/xp/test/2.jpg"; path=" ...
- JAVA NIO学习记录1-buffer和channel
什么是NIO? Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.NIO与原来的IO有同样的作用和目的,但是使用的方式完全不 ...
- 系统学习 Java IO (一)----输入流和输出流 InputStream/OutputStream
目录:系统学习 Java IO ---- 目录,概览 InputStream 是Java IO API中所有输入流的父类. 表示有序的字节流,换句话说,可以将 InputStream 中的数据作为有序 ...
- java IO流 之 字节流
一.file类的常用操作 File file=new File("E:\\test\\javaIo"); System.out.println(file.isDirectory() ...
- [Java] Java IO Files
Files 使用 FileInputStream 或 FileReader 可以用于读入文件,前者基于二进制,后者基于文本.使用它们不需要读取整个文件,但是只能按照它们存储的顺序,依次读取字节,或字符 ...
- [Java] Java IO 概况
Java IO 是 Java 的一套 API, 用于读入和写出数据(输入和输出).Java IO API 位于 java.io package.实际上 java.io package 没有解决所有的输 ...
- 系统学习 Java IO (十三)----字符读写 Reader/Writer 及其常用子类
目录:系统学习 Java IO---- 目录,概览 Reader Reader 类是 Java IO API 中所有 Reader 子类的基类. Reader 类似于 InputStream ,除了它 ...
- 系统学习 Java IO (三)----文件类 File
目录:系统学习 Java IO---- 目录,概览 Java IO API 中的 File 类可以访问基础文件系统. 使用 File 类,可以: 检查文件或目录是否存在. 如果目录不存在,创建一个目录 ...
- Java IO: OutputStream
原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) OutputStream类是Java IO API中所有输出流的基类.子类包括Buffere ...
随机推荐
- 【Mybatis】MyBatis之Sql配置文件的使用(四)
上一章[Mybatis]MyBatis对表执行CRUD操作(三),已经讲了基本操作,本章介绍Sql配置文件中常用功能 1.插入返回主键 2.参数值的获取方式 3.resultMap使用 插入返回主键 ...
- gson的特殊用法
1.gson包在处理 字符串转 Map 或者 List 的方法. List memberList = gson.fromJson(str,new TypeToken<List>() {}. ...
- c# 将object尝试转为指定对象
主方法: /// <summary> /// 将object尝试转为指定对象 /// </summary> /// <param name="data" ...
- 第一次OO总结
作业1——多项式加减法 看到这个名字就开始瑟瑟发抖了,毕竟一年前用C语言让我写这么一个程序都很头疼,什么堆栈啊还有结构都稀里糊涂的,更别说用一个完全没接触过的语言来完成最简单的一次作业.像我这样越老心 ...
- 分享几个写 demo 的思路
好久没有动笔,最近发现了一个新的写 demo 的思路,非常有意思.仔细一想,自己仿佛积累了不少写 demo 的思路和想法,总结一下,抛砖引玉. 本文所说 demo 主要分以下三种: 本地 demo 外 ...
- window.open()新开网页被拦截
问题:同一个项目,同一个浏览器,不同模块,相同的代码(同是window.open()),为何一个直接打开,另一个直接被拦截? 原因:查资料发现为浏览器的广告拦截功能导致. 补充: 1.一般情况下,js ...
- C语言编程遇到的问题
1.内存泄漏问题 问题代码1 #include <stdio.h> #include <stdlib.h> int main( int argc, char *argv[] ) ...
- Java 8 特性
1.简介 毫无疑问,Java 8是自Java 5(2004年)发布以来Java语言最大的一次版本升级,Java 8带来了很多的新特性,比如编译器.类库.开发工具和JVM(Java虚拟机).在这篇教程 ...
- Python环境安装及IDE介绍
因为最近时间比较松散,公司的业务也不多,所以想趁机赶紧投入到人工智能的学习大业当中.经过多次比较,看到目前市面上还是使用Python做为基础语言较多,进儿学习算法.人工智能组件.机器学习.数据挖掘等课 ...
- 深入-FastReport TfrxReport组件使用
[翻译] FastReport TfrxReport组件使用 一:加载和保存报表 报表默认保存在项目窗体文件中,大多数情况下,没有更多的操作要深圳市, 因此,你不需要采取特别措施来载入报告.如果你 ...