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 ...
随机推荐
- 枚举在c与c++中定义的不同
众所周知的,枚举是在运行期才决定枚举变量的值的,而不是像宏一样在预编译的时候就进行值得替换. 而且c标准规定: size(int) <= size(enum)<=系统所能表示的最大范围的值 ...
- Docker系列(五)OVS+Docker网络打通示例
环境说明 两个虚拟机 操作系统Centos7 DOcker版本1.8 脚本内容: 1 4 7 10 19 27 32 33 39 -j ACCEPT 47 48 # R ...
- 使用WIF实现单点登录Part III —— 正式实战 -摘自网络
经过前两篇文章,估计大家对WIF已经有比较充分的认识了,估计大家在经过了枯燥的理论以后,肯定是摩拳擦掌赶紧付诸于行动了.没办法,咱们程序员就是这个毛病.那好吧,我也不那么多废话了,直接进入正题吧. 我 ...
- What's the difference between all the Selection Segues
relationship -A "relationship" segue is the segue between a container view controller and ...
- 注册表-恶意首页追踪之旅(IE不能改主页)
恶意首页追踪之旅(先说下,360无法修复这个恶意首页) 话说,今天下了个扫站的工具,结果一不小心中了恶意广告! 中招后不停的乱下东西安装,360不停的在那弹出提示! 无语了,一个个卸载,把C:\win ...
- STM32F407 ADC DMA 采样实验
转载:http://home.eeworld.com.cn/my/space-uid-361439-blogid-239703.html STM32F407ADC采样实验 热度 1已有 5472 次阅 ...
- 实现GetHashCode时要遵循的规则
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:实现GetHashCode时要遵循的规则.
- Linq to SQL 简单增删改查
用Linq大大减少了对数据库的一般操作所需的编码量.运行下面事例之前,首先建一个叫做Alien的数据库表. CREATE TABLE [dbo].[Aliens]( [Id] [int] IDE ...
- UML类图中类与类的四种关系图解
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 基于smack的xmpp packet 重写
基于Smack 实现Notification数据包.smack的类中有一个org.jivesoftware.smack.packet.IQ只需对他重写即可,在做的时候其实可以简单一点的,如果你使用ti ...