java(I/O)文件读写

本文介绍了使用Java进行文件读写的几种方法,包括普通写入、追加写入及随机访问写入等,并提供了完整的示例代码。通过这些方法,可以轻松实现数据的持久化存储。

上面我们讲了Hello World!,我们完成了一个程序,得到的结果该如何保存,总不能每次开机都要运行一次吧?所以我们需要一个载体把我们的结果记录下来。下面我就介绍一个方便的记录方式,文件读写。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class TestFile {
	
	 public TestFile(){
		 
		 
	 }
	
	 
	 
	 public void appendWriterFile(String fileName,String content){	 
		 try {
			FileWriter writer = new FileWriter(fileName,true);
			writer.write(content);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	 }
	 
	 public void writerFile(String fileName,String content){
				 
		 try {
			FileWriter writer = new FileWriter(fileName);
			writer.write(content);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	 }
	 

	 public String readFile(String fileName){
		 StringBuffer results = new StringBuffer();
		 File file = new File(fileName);  
		 BufferedReader reader = null;
	     try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			 while ((tempString = reader.readLine()) != null) { 
				 results.append(tempString+"\r\n");
			 }
			 reader.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {  
            if (reader != null) {  
                try {  
                    reader.close();  
                } catch (IOException e1) {  
                }  
            }  
        }
		return results.toString();    
         	 
	 }
	 
	 public void randomWriterFile(String fileName,String content){
		 
		 try {
			RandomAccessFile writer = new RandomAccessFile(fileName, "rw"); 
			long length = writer.length();
			writer.seek(length);
			writer.writeBytes(content);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	 }
	 
	 public String randomReadFile(String fileName){
		 StringBuffer results = new StringBuffer();
		 RandomAccessFile reader = null; 
	     try {
			reader = new RandomAccessFile(fileName, "r"); 
			long length = reader.length();  
            // 读文件的起始位置  
            int index = (length > 4) ? 4 : 0;  
            // 将读文件的开始位置移到beginIndex位置。  
            reader.seek(index);  
			String tempString = null;
			 while ((tempString = reader.readLine()) != null) { 
				 results.append(tempString+"\r\n");
			 }
			 reader.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {  
            if (reader != null) {  
                try {  
                    reader.close();  
                } catch (IOException e1) {  
                }  
            }  
        }
		return results.toString();    
         	 
	 }
	
	 public static void main(String[] args) { 
		 String fileName="/Users/victor/test.txt";
		 String content="Hello World!\r\n";   // 加入换行符 \r\n
		 
		 TestFile test =new TestFile();
		 //写入文件
		 test.writerFile(fileName, content);
		 test.appendWriterFile(fileName, content);  //追加写入
		 //读取文件
		 String results = test.readFile(fileName);
		 System.out.println(results);		  
		 
	 }

}


快速的学会文件的读写,这些能满足大家大部分需求,如果要对流操作请查api。


文章引用 2dot5


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值