对txt文件内容修改存入新txt文件
2010-05-23 10:51
489 查看
该程序是将txt文件中形如“/w”这样的字符串去掉,遇到空格换行,网址形式则不作任何处理,并且去掉标点符号。修改后存入新的txt文件中。
package com; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.RandomAccessFile; public class OutputBySort { public String readFromFile(File file) throws IOException {//读取文件并且转化 FileInputStream fis = null; try { fis = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } InputStreamReader ipsr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(ipsr);//创建bufferReader对象 String str = null; String str1 = null; String str2 = null; String str3 = null; int pos,length; StringBuffer sb = new StringBuffer();//创建stringBuffer try { while ((str = br.readLine()) != null) {//读一行 sb.append(str);//将读到的一行str加入到string缓冲区 sb.append("/r/n");//读入换行符 } } catch (IOException e) { e.printStackTrace(); } str1 = sb.toString();//转化为String str1 = str1.replaceAll("//D"+"/w","");//去标点 str1 = str1.replaceAll("/[a-zA-Z]+", "");//将/w类似的子串去掉 pos = str1.indexOf("html");//html的位置 length = str1.length(); str2 = str1.substring(0, pos+5);//网址 str2 = str2.replaceAll("//s+",""); str3 = str1.substring(pos+4, length);//网址以后的内容 str3 = str3.replaceAll("//s+", "/r/n");//将空格转化为换行 str3 = str2 + str3; return str3; } public void writeIntoFile(File file,String str)throws IOException{//写入到文件,str为要写入的String try{ String str1 = str; RandomAccessFile dos = new RandomAccessFile(file, "rw");//打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件 dos.seek(dos.length()); dos.write(str1.getBytes()); }catch(IOException e){ e.printStackTrace(); } } public void creatFolder(String folderPath){//创建新目录 try { File myFilePath = new File(folderPath); if (!myFilePath.exists()) {//不存在则重新创建一个 myFilePath.mkdir(); } } catch (Exception e) { e.printStackTrace(); } } public void Transformation(String oldFolderPath,String newFolderPath)throws IOException{//转化方法 String oldPath = oldFolderPath;//原有文件夹路径 String newPath = newFolderPath;//新文件路径 String str,string;//str用于接收readFromFile返回字符,string用于写入文件的文件路径 File file = new File(oldPath);//原来文件夹的对象 File[] childs = file.listFiles();//获得文件夹下所有子文件 creatFolder(newPath);//创建新目录 for(int i = 0;i<childs.length;i++) { string = childs[i].getName();//原名 string = newPath +"/"+ string;//写入的文件路径(新路径+原名) str = readFromFile(childs[i]); File f = new File(string); writeIntoFile(f,str); } } public static void main(String[] args) throws IOException{ OutputBySort out = new OutputBySort(); out.Transformation("E:/a", "E:/c"); } }
相关文章推荐
- [zz]怎样用C#读取TXT文件内容并修改
- 内容文件Java创建TXT文件并进行读、写、修改操作
- txt文件内容修改
- 关于python的txt文件内容修改的解决方案
- 编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt
- C++读取txt文件内容——每行按列存入数组
- 从键盘输入若干个字符,逐个存到磁盘文件data.txt中,直到输入’\n’为止,并将建立的文本文件的内容在存入文件的同时,在屏幕上显示出来:
- 如何修改txt文本文件的内容
- java对txt文本文件的复制粘贴(对文件内容进行了简单的修改)以行为一个处理单位
- python修改txt文件内容
- java创建txt文件并存入内容
- 简洁的从txt文件中读取数据并且修改数据重新存入
- java创建txt文件并存入内容
- 2,MySql数据库存入乱码,查询出来全改成uft-8,修改mysql配置文件my.ini内容为:default-character-set=utf8 查询下面
- 【ASP.NET 进阶】定时执行任务实现 (定时读取和修改txt文件数字内容,无刷新显示结果)
- 根据条件从txt文件中挑出一些文字并据此修改文件名字
- android中读取txt文件内容防止出现乱码
- linux下,用c语言编程,实现从一个文件读取内容到另一个文件,可以在文件的不同位置修改文件内容,并保存。
- JAVA 创建TXT文件,写入文件内容,读取文件内容
- R语言读取txt文件中的内容