java读写文件范例

public class ReadfromFile {
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件
*/
public static void readfileByBytes(String filename){
File file = new File(filename);
InputStream ins = null;
try {
ins = new FileInputStream(file);
int tmpbyte;
try {
while((tmpbyte =ins.read()) != -1 ){ // 以字节为单位一次只读一个字节
if((char)tmpbyte != '\r')
  System.out.print((char)tmpbyte);
}
System.out.println();
ins.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
try {
ins = new FileInputStream(file);
byte[] tmpbytes = new byte[1024];
int tmp = 0;
try {
while((tmp = ins.read(tmpbytes)) != -1){
if((char)tmpbytes[tmpbytes.length -1] != '\r'){ // 以字节为单位一次读取多个字节
for (int i = 0; i < tmp; i++) {
System.out.print((char)tmpbytes[i]);
}
}
}
ins.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
/**
* 以字符为单位读取文件,常用于读取文本、数字等类型的文件
* @param args
*/
public static void readfileByChar(String filename){
File file = new File(filename);
Reader read = null;
try {
read = new InputStreamReader(new FileInputStream(file));
int tmpchar = 0;
try {
while((tmpchar = read.read()) != -1){ // 以字符为单位读取,一次读取一个字节
if((char)tmpchar != '\r')
  System.out.print((char)tmpchar);
}
System.out.println();
read.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
try {
read = new InputStreamReader(new FileInputStream(file));
char[] tmpchar = new char[100];
int tmp = 0;
try {
while((tmp = read.read(tmpchar))!=-1){ // 以字符文单位读取文件,一次读取多个字节
if(tmpchar[tmpchar.length -1] != '\r'){
for (int i = 0; i < tmp; i++) {
System.out.print(tmpchar[i]);
}
}
}
read.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
/**
* 以行为单位读取文件,适用于读取面向行的格式化文件
* @param args
*/
public static void readfileByLine(String filename){
File file = new File(filename);
BufferedReader buread = null;
String tmpstring = null;
try {
int line = 1;
buread = new BufferedReader(new FileReader(file));
try {
while((tmpstring =buread.readLine()) != null){ // 一次读入一行一直到文件为null为止
System.out.println(tmpstring);
line++;
}
buread.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
/**
* 文件写操作
* @param args
*/
public static void writefile(String filename){
File file = new File(filename);
FileOutputStream ous = null;
FileOutputStream fous = null;
BufferedOutputStream bos = null;
FileWriter fw = null;
int b = 10;
try {
ous = new FileOutputStream(file);
try {
long begin = System.currentTimeMillis();
for (int i = 0; i < b; i++) {
ous.write("测试java写文件操作\r\n".getBytes());
}
ous.close();
long end = System.currentTimeMillis();
System.out.println("FileOutputStream执行耗时:"+(end - begin)+"毫秒");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
try {
fous = new FileOutputStream(file);
bos = new BufferedOutputStream(fous);
try {
long begin = System.currentTimeMillis();
for (int i = 0; i < b; i++) {  
bos.write("测试java写文件操作\r\n".getBytes());
}
bos.flush();
bos.close();
long end = System.currentTimeMillis();
System.out.println("BufferedOutputStream执行耗时:"+(end - begin)+"毫秒");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fw = new FileWriter(file);
long begin = System.currentTimeMillis();
for (int i = 0; i < b; i++) {
fw.write("测试java写文件操作");
}
fw.close();
long end = System.currentTimeMillis();
System.out.println("FileWriter执行耗时:"+(end - begin)+"毫秒");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

猜你喜欢

转载自hyl123456.iteye.com/blog/2236215