Java操作文本封装类

Python015

Java操作文本封装类,第1张

import java io BufferedReader

import java io BufferedWriter

import java io File

import java io FileReader

import java io FileWriter

import java io IOException

import java util ArrayList

import java util List

/**

* 用于对记事本的操作

*

* @author 沙琪玛

*

*/

public class NoteOperate {

// txt文件路径

private String filePath

/**

* 构造函数

*

* @param filePath

*            文本文件全路径

*/

public NoteOperate(String filePath) {

this filePath = filePath

}

/**

* 构造函数

*

* @param file

*            需要读取的文本文件

*/

public NoteOperate(File file) {

this filePath = file getPath()

}

/**

* 判断文本文件是否存在

*

* @return 存在返回true 否则返回false

*/

public boolean exists() {

File file = new File(this filePath)

return file exists()

}

/**

* 得到这个txt所有的列的数据 空行将自动跳过 并自动删除文字前后的空格

*

* @return List<String>

* @throws IOException

*/

public List<String>fileLinesContent() throws IOException {

List<String>strs = new ArrayList<String>()

File file = new File(this filePath)

FileReader fr = new FileReader(file)// 建立FileReader对象 并实例化为fr

BufferedReader br = new BufferedReader(fr)// 建立BufferedReader对象 并实例化为br

String Line = br readLine()// 从文件读取一行字符串

// 判断读取到的字符串是否不为空

while (Line != null) {

if (! equals(Line))

strs add(Line trim())

Line = br readLine()// 从文件中继续读取一行数据

}

br close()// 关闭BufferedReader对象

fr close()// 关闭文件

return strs

}

/**

* 创建一个空的记事本文档 如果这个记事本文档存在就不再创建 函数还未写实现部分<br/>如果文本已经存在则不再创建

*

* @throws IOException

*/

public void createEmptyNote() throws IOException {

File file = new File(this filePath)

if (!file exists())

file createNewFile()

}

/**

* 将内容写入这个文本 注意以前的内容将会被删除

*

* @param str

*            将要写入的内容

* @throws IOException

*/

public void writeString(String str) throws IOException {

File file = new File(this filePath)

BufferedWriter output = new BufferedWriter(new FileWriter(file))

output write(str)

output close()// 关闭BufferedReader对象

}

/**

* 在文本的指定行插入文字 如果该行已经存在 该行内容将会被删除 如果行号不存在 将会 *** 入到最后一行

*

* @param i

*            行号 行号为 时 将插入到最后一行

* @param str

*            将要插入的内容

* @throws IOException

*/

public void insertWords(int i String str) throws IOException {

List<String>strs = fileLinesContent()

// 进行插入操作

if (i == || strs size() <i) // 插入到最后一行

{

strs add(str)

} else { // 插入到文本中

strs set(i str)

}

// 重新写入到文本

StringBuffer *** = new StringBuffer()

for (String temp : strs) {

*** append(temp replace( \r\n )+ \r\n )

}

writeString( *** toString())

}

lishixinzhi/Article/program/Java/hx/201311/26945

很简单撒。利用HashMap不能重复的特点就可以了。

你先读b.txt的数据到HashMap.

然后再读a.txt 到同一个Map

这个时候如果遇到重复的Key,value就会被覆盖的。

就达到你的要求了撒。

参考代码:

将A.txt B.txt 换成你自己的路劲就可以了。

package a

import java.io.BufferedReader

import java.io.File

import java.io.FileInputStream

import java.io.FileOutputStream

import java.io.IOException

import java.io.InputStreamReader

import java.nio.ByteBuffer

import java.nio.channels.FileChannel

import java.util.HashMap

import java.util.Iterator

import java.util.Map

import java.util.Map.Entry

public class TestReturn {

private static HashMap<String, String> map=new HashMap<String, String>()

public static void getTxt(File file){

try {

if(file.isFile() && file.exists()){ //判断文件是否存在

            InputStreamReader read = new InputStreamReader(

            new FileInputStream(file))

            BufferedReader bufferedReader = new BufferedReader(read)

            String lineTxt = null

            while((lineTxt = bufferedReader.readLine()) != null){

                System.out.println(lineTxt)

                map.put(lineTxt.split(",")[0], lineTxt.split(",")[1])

            }

            read.close()

        }

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace()

}

}

public static void writeTxt(File file) throws IOException

{

if(!file.exists())

file.createNewFile()

        FileOutputStream outputStream = new FileOutputStream(file)

        FileChannel channel = outputStream.getChannel()

        String content=""

        Iterator iter = map.entrySet().iterator()

        while(iter.hasNext()){

         Map.Entry<String, String> entry= (Entry<String, String>) iter.next()

         content+=entry.getKey()+","

         content+=entry.getValue()+"\n" 

        }

        ByteBuffer buffer = ByteBuffer.allocate(content.getBytes().length)

        buffer.put(content.getBytes())

        buffer.flip()     //此处必须要调用buffer的flip方法

        channel.write(buffer)

        channel.close()

        outputStream.close()

}

public static void main(String[] args) throws IOException {

//处理文件B

getTxt(new File("d://B.txt"))

//处理文件A

getTxt(new File("d://A.txt"))

//数写入C

writeTxt(new File("d://C.txt"))

}

}