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 ...
随机推荐
- linux的常用指令和配置文件
一. 常用的指令 mkdir -p 创建文件夹 parents递归创建 ls -alh 查看当前目录内容 cd 切换工作目录 pwd 打印当前工作目录 touch 文件名 创建文件 echo 字符 ...
- 冒泡排序&&选择排序
package cn.lijun.demo;//冒泡排序public class Test5 { public static void main(String[] args) { int[] arr ...
- Unity使用代码动态给按钮赋值各个状态下的图片
一个小知识点,怕忘记,所以记录下.废话不多说,直接上代码: 未赋值之前: 使用下面代码赋值: using UnityEngine; using UnityEngine.UI; public class ...
- ASP.NET代码调用SQL Server带DateTime类型参数的存储过程抛出异常问题
ASP.NET代码调用SQL Server带DateTime类型参数的存储过程,如果DateTime类型参数的值是'0001/1/1 0:00:00'时,就会抛出异常“Message: SqlDate ...
- Notes : <Hands-on ML with Sklearn & TF> Chapter 3
Chapter 3-Classification .caret, .dropup > .btn > .caret { border-top-color: #000 !important; ...
- macOS 安装 ctags
macOS 安装 ctags macOS 自带一个 ctags,但是不支持 -R 参数,递归产生tags文件 $ ctags -R --exclude=.git --exclude=log * cta ...
- Java中的一个类型转换问题
一.Object转Integer Java中hibernate或者ResultSetHandler查询sql语句, 返回的object类型其实是Long类型, 而不是Integer类型, 因此此时直接 ...
- iOS 数据归档----温故而知新
#import "StudyViewController.h" #import "person.h" @interface StudyViewControlle ...
- 运维工具pssh和pdsh安装和使用
1. pssh安装与使用 1.1 pssh安装 [root@server]# wget http://peak.telecommunity.com/dist/ez_setup.py [root@ser ...
- RabbitMQ Routing 消息路由
上篇文章中,我们构建了一个简单的日志系统.接下来,我们将丰富它:能够使用不同的severity来监听不同等级的log.比如我们希望只有error的log才保存到磁盘上. 1. Bindings绑定 上 ...