java写入写出流,(部分)

来源:互联网 发布:达内java课程安排 编辑:程序博客网 时间:2024/06/05 21:50
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();
}
}
1 0
原创粉丝点击