java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
一,File创建文件
File file = new File("D:" + File.separator + "yi.txt");
代码示例:
package com.hbut.io; import java.io.File;
import java.io.IOException; public class FileDemo { public static void main(String[] args) {
File file = new File("D:" + File.separator + "yi.txt");
if (file.exists()) {
file.delete();
System.out.println("文件已存在");
} else {
try {
boolean states=false;
states = file.createNewFile();
if(states==true)
{
System.out.println("文件创建成功");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
二,OutputStream(字节流)写入文件
out= new FileOutputStream(file);//获取实际的字节流输出对象,内容覆盖
String info="hello";//要输入的内容
byte[] b=info.getBytes();//将字符转化为字节数组
out.write(b);
代码示例
package com.hbut.io; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; public class OutputDemo {
public static void main(String[] args) {
File file = new File("D:" + File.separator + "yi.txt");//指定要写入的文件
OutputStream out=null;//定义字节流输出对象
try {
out= new FileOutputStream(file);//获取实际的字节流输出对象,内容覆盖
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String info="hello";//要输入的内容
byte[] b=info.getBytes();//将字符转化为字节数组
try {
out.write(b);
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三,InputStream(字节流)读出文件
InputStream In = null;// 定义字节流输入对象
In = new FileInputStream(file);// 获取实际的字节流输入对象
byte[] b = new byte[1024];// 开辟空间,读取内容
len = In.read(b);// 读取
System.out.println(new String(b, 0, len));//输出读取的内容
代码示例
package com.hbut.io; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; public class InputStreamDemo {
public static void main(String[] args) {
File file = new File("D:" + File.separator + "yi.txt");// 指定要读取的文件
InputStream In = null;// 定义字节流输入对象
try {
// out= new FileOutputStream(file,true);//是否字节追加函数
In = new FileInputStream(file);// 获取实际的字节流输入对象
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int len = 0;// 输入数组长度
byte[] b = new byte[1024];// 开辟空间,读取内容
// byte[] b=new byte[(int)file.length()];//根据文件大小开辟空间
try {
len = In.read(b);// 读取
} catch (IOException e1) {
e1.printStackTrace();
}
try {
In.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(new String(b, 0, len));
}
}
四,Writer(字符流)写入文件
Writer write = null;// 定义字符输出流
write = new FileWriter(file);
String infor = "hello,WriterDemo";//即将写入文件的信息
write.write(infor);//写入文件
代码示例
package com.hbut.io; import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer; public class WriterDemo {
public static void main(String[] args) {
File file = new File("D:" + File.separator + "WriterDemo.txt");// 指定要写入的文件
Writer write = null;// 定义字符输出流
try {
write = new FileWriter(file);
} catch (IOException e) {
e.printStackTrace();
}
String infor = "hello,WriterDemo";
try {
write.write(infor);
} catch (IOException e) {
e.printStackTrace();
}
try {
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
五,Reader(字符流)读出文件内容
Reader read = null;// 定义字符输入流
read = new FileReader(file);
char[] b=new char[1024];//设置字符的长度
int len=read.read(b);//文件内容读入到 b[]
System.out.println(new String(b));
代码示例
package com.hbut.io; import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader; public class ReaderDemo {
public static void main(String[] args) {
File file = new File("D:" + File.separator + "yi.txt");// 指定要读出的文件
Reader read = null;// 定义字符输入流
try {
read = new FileReader(file);
} catch (IOException e) {
e.printStackTrace();
}
char[] b=new char[1024];//设置字符的长度
try {
int len=read.read(b);
System.out.println(new String(b));
} catch (IOException e) {
e.printStackTrace();
}
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java IO文件读写例子(OutputStream,InputStream,Writer,Reader)的更多相关文章
- Java IO--字节流与字符流OutputStream/InputStream/Writer/Reader
流的概念 程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件. 字节流与字符流 内容操作就四个类:OutputStream.InputStream.Writer.Reader 字节流 ...
- java io 文件读写操作
写: import java.io.*; String filePath= "F:\\test.txt"; FileWriter fwriter = null; fwriter = ...
- Java IO流操作汇总: inputStream 和 outputStream【转】
我们在进行Android java 开发的时候,经常会遇到各种IO流操作.IO流操作一般分为两类:字符流和字节流.以“Reader”结尾都是字符流,操作的都是字符型的数据:以“Stream”结尾的都是 ...
- 沉淀再出发:java的文件读写
沉淀再出发:java的文件读写 一.前言 对于java的文件读写是我们必须使用的一项基本技能,因此了解其中的原理,字节流和字符流的本质有着重要的意义. 二.java中的I/O操作 2.1.文件读写的本 ...
- Java IO 文件与流基础
Java IO 文件与流基础 @author ixenos 摘要:创建文件.文件过滤.流分类.流结构.常见流.文件流.字节数组流(缓冲区) 如何创建一个文件 #当我们调用File类的构造器时,仅仅是在 ...
- Java IO 节点流 FileInput/OutputStream
Java IO 节点流 FileInput/OutputStream @author ixenos 节点流之 文件流 文件读写是最常见的I/O操作,通过文件流来连接磁盘文件,读写文件内容 1.文件的读 ...
- 一头扎进 Java IO中-------java IO文件
Java IO: 文件 在Java应用程序中,文件是一种常用的数据源或者存储数据的媒介.所以这一小节将会对Java中文件的使用做一个简短的概述.这篇文章不会对每一个技术细节都做出解释,而是会针对文件存 ...
- Java IO 过滤流 BufferedInput/OutputStream
Java IO 过滤流 BufferedInput/OutputStream @author ixenos 概念 BufferedInput/OutputStream是实现缓存的过滤流,他们分别是Fi ...
- spark 执行报错 java.io.EOFException: Premature EOF from inputStream
使用spark2.4跟spark2.3 做替代公司现有的hive选项. 跑个别任务spark有以下错误 java.io.EOFException: Premature EOF from inputSt ...
随机推荐
- shell脚本应用(5)--实用脚本片段
校验参数 if [ "$#" -ne 1 ] then echo "Incorrect number of arguments" echo "Usag ...
- C++中构造函数和析构函数调用的时机
今天看书忽然对这个地方有点模糊,尤其是析构函数在调用默认的析构函数和用户自己覆写的析构函数的时候有点意识模糊呢.写段代码总结下 #include <iostream> using name ...
- hadoop入门必备基础知识
1.对Linux 系统的要求 会基本的命令: (1)知道root用户 (2)ls命令会查看文件夹内容 (3)cd命令等2.Java 的要求 ...
- Android UI开发第三十四篇——SlidingPaneLayout
SlidingPaneLayout也是系统支持的高级控件,是Android团对在2013 google IO大会期间更新的Support库(Version 13)中新加入的重要的功能.它支持左右滑动菜 ...
- ScheduledExecutorFactoryBean忽略异常继续执行
ScheduledExecutorFactoryBean忽略异常继续执行 程序中有一个定时任务,每10分钟把满足条件的任务从一个表迁移到另一张表,程序启动的时候数据库异常了一段时间,之后数据库恢复了. ...
- 包含块、层叠上下文、BFC
包含块 什么是包含块?简单来说,就是决定一个元素大小和定位的元素.一个元素会为它的内部元素创建包含块,但也不能说元素的包含块就是它的父元素: 1.position:fixed 的元素 包含块是当前可视 ...
- AWS s3 python sdk code examples
Yet another easy-to-understand, easy-to-use aws s3 python sdk code examples. github地址:https://github ...
- 2假动作,数据缓冲,CCEaseExponential,CCEaseElastic,CCEaseBounce,CCCallFunc,funcNCallBack,funcNDCallBack,funcO
1 缓冲动作 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/4 ...
- 13 引用WINAPI
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWind ...
- Edit显示行号
Edit显示代码行号 关键点 使用这个类然后关联Edit的变量为 LineNumberEdit类型的 实现过程 //////////////////////////////////////////// ...