java写入写出流,(部分)

本文详细介绍了使用Java进行文件操作的方法,包括不同方式的文件读写技术,如字节流、字符流及缓冲读取等,并演示了如何创建文件和目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package cn.zhang.test;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;


public class FileTest2 {
public static void main(String[] args) {
File file=new File("d:"+File.separator+"three.txt");
try {
// test5(file);
test8();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 以单个字节读取,常用于读取二进制文件,如图片,声音,影像等。
* @param file
* @throws Exception 
*/
public static void test1(File file) throws Exception{
try {
InputStream in=new FileInputStream(file);
int tempByte;
while((tempByte=in.read())!=-1){
System.out.println((char)tempByte);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 已多个字节读取文件,常用于读取二进制文件,如图片,声音,影像等。
* @param file
* @throws Exception
*/
public static void test2(File file) throws Exception{
InputStream in=new FileInputStream(file);
byte tempBytes[]=new byte[400];
int len=0;
while((len=in.read(tempBytes))!=-1){
System.out.println(new String(tempBytes,0,len));
}
}
/**
* 以单个字符为单位,常用于读取文本,数字等类型的文件
* @param file
* @throws Exception
*/
public static void test3(File file) throws Exception{
Reader reader=new InputStreamReader(new FileInputStream(file));
int tempChar;
while((tempChar=reader.read())!=-1){
System.out.println((char)tempChar);
}
}
/**
*以多个字符为单位,常用于读取文本,数字等类型的文件
* @param file
* @throws Exception
*/
public static void test4(File file) throws Exception{
Reader reader=new InputStreamReader(new FileInputStream(file));
char chars[]=new char[1000];
int len;
while((len=reader.read(chars))!=-1){
System.out.println(new String(chars,0,len));
}
}
/**
* 一次读取一行,常用于格式化行的文件
* @param file
* @throws Exception
*/
public static void test5(File file)throws Exception{
BufferedReader reader=new BufferedReader(new FileReader(file));
String tempString=null;
while((tempString=reader.readLine())!=null){
System.out.println(tempString);
}
}
/**
* 创建文件和文件夹
* @throws Exception
*/
public static void test6()throws Exception{
File file=new File("d:"+File.separator+"four.txt");
if(!file.exists()){
file.createNewFile();
}
File file2=new File("d:"+File.separator+"goDie");
if(!file2.exists()){
file2.mkdir();
}
}

/**
*  字节流读字符流写
* @throws Exception
*/
public static void test7() throws Exception{
File file=new File("D:"+File.separator+"three.txt");
InputStream in=new FileInputStream(file);
File file2=new File("d:"+File.separator+"four.txt");
if(!file2.exists()){
file2.createNewFile();
}
OutputStream out=new FileOutputStream(file2);
int ch=0;
byte []bytes=new byte[1024];
while((ch=in.read(bytes))!=-1){
out.write(bytes);
}
in.close();
out.close();
}
/**
* 字符流读,字符流写
* @throws Exception
*/
public static void test8()throws Exception{
File file=new File("d:"+File.separator+"three.txt");
Reader reader=new InputStreamReader(new FileInputStream(file));

File file2=new File("d:"+File.separator+"five.txt");
if(!file2.exists()){
file2.createNewFile();
}
FileWriter writer=new FileWriter(file2);
char []chars=new char[1024];
int len;
while((len=reader.read(chars))!=-1){
writer.write(chars);
}
reader.close();
writer.close();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值