---
title: "自己总结的Java实现文件的读写操作"
author: "abeetle"
date: "2007-12-20T14:23:00Z"
tags: []
slug: "java"
draft: false
lastmod: "2007-12-20T14:23:00Z"
id: 233838
url: "https://abeetle.iteye.com/blog/233838"
---

做个笔记,针对java 的file的操作

 **1.根据文件名及字符串,写文件**

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public static boolean writeJiang(String content,String path,String name)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            File file = new File(path);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.println("文件的路径是：" + file+"/"+name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            BufferedWriter fileout = new BufferedWriter(new FileWriter(file+"/"+name,true));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            fileout.write(content);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            fileout.write(" ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            fileout.flush();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            fileout.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        catch(Exception e)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println("写文件完毕");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

**2.读文件,返回字符串**

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public static String readTxt(String path)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String array = "";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            //读取文本文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            File file = new File(path);          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            FileInputStream rdf = new FileInputStream(file);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            byte\[] s = new byte\[rdf.available()];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            int b = rdf.available();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            while((b=rdf.read(s, 0, b))!=-1)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                String content = new String(s,0,b);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                array = array+content;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            rdf.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        catch(Exception e)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return array;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

3.删除文件中某一行.ID是行号,path+name=File

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public static int deleteTxt(String path,String name,int id)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int result = 0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String content = "";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            //读取文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            content = readTxt(path + "/" + name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.println(path + "/" + name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.println("未删除记录之前得是：" +  content);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            //删除某一行  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            String\[] a = content.split(" ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            StringBuffer d = new StringBuffer();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            for (int j = 0; j &lt; a.length; j++) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                if (j != id)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    d.append(a\[j]).append(" ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.println("删除记录之后的为：" + d.toString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            //将修改后的写入文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            writeNewTxt(d.toString(),path,name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            result = 1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        catch(Exception e)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return result;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }