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 ...
随机推荐
- 图解HTTP第九章
基于 HTTP 的功能追加协议 1>HTTP 的瓶颈有哪些: 2>消除 HTTP 瓶颈的 SPDY,缩短 Web 页面的加载时间 [1]SPDY 的设计与功能 [2]SPDY 消除 Web ...
- docker私库Harbor部署(转载)
系统环境 centos7.3docker-ce docker version: 18.03.0docker-compose version: 1.21.0 Install Docker CE 安装依赖 ...
- MySQL优化:使用show status查看MySQL服务器状态信息
在网站开发过程中,有些时候我们需要了解MySQL的服务器状态信息,譬如当前MySQL启动后的运行时间,当前MySQL的客户端会话连接数,当前MySQL服务器执行的慢查询数,当前MySQL执行了多少SE ...
- 2T以上磁盘格式化
1.安装软件 对于 Debian/Ubuntu 用户, 使用 APT-GET 命令或者 APT 命令来安装 parted #apt-get install -y parted 对于 RHEL/Cent ...
- Java面试题之Redis
1.redis数据结构有哪些? string,list,hash,set,zset 2.redis为什么是单线程的? redis是基于内存的操作,cpu不是redis的瓶颈,内存大小或网络带宽才是: ...
- 大数据项目测试<二>项目的测试工作
大数据的测试工作: 1.模块的单独测试 2.模块间的联调测试 3.系统的性能测试:内存泄露.磁盘占用.计算效率 4.数据验证(核心) 下面对各个模块的测试工作进行单独讲解. 0. 功能测试 1. 性能 ...
- Mybatis第二天
Mybatis第二天 框架课程 1. 课程计划 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If标签 b) Where标签 c) Sql片段 d) Fore ...
- prim算法和克鲁斯卡尔算法
Prim 设图G=(V,E)是一个具有n个顶点的连通网,其生成树的顶点集合为U.首先把v0放入U,再在所有的u∈U,v∈V-U的边(u,v)∈E中找一条最小权值的边,加入生成树,并把该边的v加入U集合 ...
- scrapy的基础概念和流程
1. 什么是scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,我们只需要实现少量的代码,就能够快速的抓取. Scrapy 使用了Twisted['twɪstɪd]异步网 ...
- html、css基础整理
1.块元素与行内元素之间的转换: HTML可以将元素分类方式分为行内元素.块状元素和行内块状元素三种.这三者是可以互相转换的,使用display属性能够将三者任意转换: (1)display:inli ...