java文件操作之文字写入问题

水青_ 2017-08-11 09:29:43
各位大佬,小弟遇见一个问题,现在需要每次从一个文件的倒数第二行写入文字,不知道怎么可以定位到倒数第二行,还请各位大佬不吝赐教
...全文
173 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Freefish1994 2017-08-11
  • 打赏
  • 举报
回复
写了一个楼主可以试一下

public class Test {
	/*
	 * 原始文本    新文本
	 * 1                 1
	 * 2                 2
	 * 3                 3
	 * 4                 XXXXXX
	 *                    4
	 */
	public static void main(String[] args) {
		String txtPath = "E:/1.txt";// 文件路径
		String addContent = "XXXXXX";// 加入的内容
		List<String> contentByLine = readTxtLine(txtPath);
		contentByLine.add(contentByLine.size() - 1, addContent);//把添加的内容加到倒数第二行
		writeContent(txtPath, contentByLine);
	}

	private static List<String> readTxtLine(String txtPath) {
		String lineContent = null;
		InputStream in = null;
		InputStreamReader read = null;
		BufferedReader reader = null;
		List<String> strs = new ArrayList<String>();
		try {
			File txtFile = new File(txtPath);
			in = new FileInputStream(txtFile);
			read = new InputStreamReader(in, "GBK");
			reader = new BufferedReader(read);
			while ((lineContent = reader.readLine()) != null)// 将文本内容逐行插入List中
				strs.add(lineContent);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				read.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		return strs;
	}

	private static void writeContent(String filePath, List<String> newContent) {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(filePath);
			out.write("".getBytes());
			for (String entity : newContent) {
				entity += "\r\n";// 添加换行符
				out.write(entity.getBytes());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
天空海阔66 2017-08-11
  • 打赏
  • 举报
回复

import java.io.*;
public class readAndWrite
	{
		private BufferedReader bufferReadin=new BufferedReader(new FileReader("D:/TestR.txt"));
		private PrintWriter bufferWriter=new PrintWriter( new BufferedWriter(new FileWriter("D:/TestWrite.txt")));
		String[] sArray=new String[20];
		String s=null;
		int iline=0;//行数
		public readAndWrite()throws IOException
		{
		while((s=bufferReadin.readLine())!=null)//先读一遍统计行数
		{
			System.out.println(s);
			sArray[iline]=s;
			iline++;
		}
		int ilastSecondLine=iline-1;
		iline=0;
		while((s=sArray[iline])!=null)
		{
			if(ilastSecondLine==iline)//读到倒数第二行插入
			{
				bufferWriter.println("倒数第二行插入");
			}
			System.out.println(s);
			bufferWriter.println(s);
			iline++;
		}
		bufferReadin.close();
		bufferWriter.close();
		}
		public static void main(String[] args) throws IOException
		{
			readAndWrite Test=new readAndWrite();
		}
}


Freefish1994 2017-08-11
  • 打赏
  • 举报
回复
txt文本么?比如里面有10行,从倒数第2行开始插入文字?
墨笙弘一 2017-08-11
  • 打赏
  • 举报
回复
首先怎么定位到倒数第二行?应该解决这个吧
水青_ 2017-08-11
  • 打赏
  • 举报
回复
来人啊,来人啊,来人啊
水青_ 2017-08-11
  • 打赏
  • 举报
回复
来人啊,来人啊,来人啊
水青_ 2017-08-11
  • 打赏
  • 举报
回复
引用 7 楼 qq_27762917 的回复:
写了一个楼主可以试一下

public class Test {
	/*
	 * 原始文本    新文本
	 * 1                 1
	 * 2                 2
	 * 3                 3
	 * 4                 XXXXXX
	 *                    4
	 */
	public static void main(String[] args) {
		String txtPath = "E:/1.txt";// 文件路径
		String addContent = "XXXXXX";// 加入的内容
		List<String> contentByLine = readTxtLine(txtPath);
		contentByLine.add(contentByLine.size() - 1, addContent);//把添加的内容加到倒数第二行
		writeContent(txtPath, contentByLine);
	}

	private static List<String> readTxtLine(String txtPath) {
		String lineContent = null;
		InputStream in = null;
		InputStreamReader read = null;
		BufferedReader reader = null;
		List<String> strs = new ArrayList<String>();
		try {
			File txtFile = new File(txtPath);
			in = new FileInputStream(txtFile);
			read = new InputStreamReader(in, "GBK");
			reader = new BufferedReader(read);
			while ((lineContent = reader.readLine()) != null)// 将文本内容逐行插入List中
				strs.add(lineContent);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				read.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		return strs;
	}

	private static void writeContent(String filePath, List<String> newContent) {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(filePath);
			out.write("".getBytes());
			for (String entity : newContent) {
				entity += "\r\n";// 添加换行符
				out.write(entity.getBytes());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
感谢
水青_ 2017-08-11
  • 打赏
  • 举报
回复
引用 5 楼 gansiwei 的回复:

import java.io.*;
public class readAndWrite
	{
		private BufferedReader bufferReadin=new BufferedReader(new FileReader("D:/TestR.txt"));
		private PrintWriter bufferWriter=new PrintWriter( new BufferedWriter(new FileWriter("D:/TestWrite.txt")));
		String[] sArray=new String[20];
		String s=null;
		int iline=0;//行数
		public readAndWrite()throws IOException
		{
		while((s=bufferReadin.readLine())!=null)//先读一遍统计行数
		{
			System.out.println(s);
			sArray[iline]=s;
			iline++;
		}
		int ilastSecondLine=iline-1;
		iline=0;
		while((s=sArray[iline])!=null)
		{
			if(ilastSecondLine==iline)//读到倒数第二行插入
			{
				bufferWriter.println("倒数第二行插入");
			}
			System.out.println(s);
			bufferWriter.println(s);
			iline++;
		}
		bufferReadin.close();
		bufferWriter.close();
		}
		public static void main(String[] args) throws IOException
		{
			readAndWrite Test=new readAndWrite();
		}
}


这个方法不错,但是你这样定义行数就是固定的了啊,如果不是20行是几百行那怎么办呢

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧