We often encounter such things, for example, there are names and phone numbers in a txt file. At this time, it is often necessary to extract the names and phone numbers. At this time, This can be achieved using io in Java.
Here I will not introduce the similarities and differences between byte stream and character stream in io in detail. Interested students can Baidu Baidu themselves.
Today we mainly introduce how to obtain the file content and modify the obtained file content. See below for specific case introductions.
This is the final effect to be achieved in the case, which is to directly add dividing symbols to the name and phone number.
The important thing to note here is that in this case, the original txt document is not modified directly, but a new txt file is created and new content is written.
Okay, without further ado, let’s take a look at how this case was implemented.
This case is divided into three modules: 1. File reading module, 2. Name and phone separation module, 3. File writing module
1. File reading module:
/** * 功能:Java读取txt文件的内容 * 步骤:1:先获得文件句柄 * 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取 * 3:读取到输入流后,需要读取生成字节流 * 4:一行一行的输出。readline()。 * 备注:需要考虑的是异常情况 * @param filePath */ public static String readTxtFile(String filePath) { StringBuilder content = new StringBuilder(""); try { String encoding = "UTF-8"; File file = new File(filePath); if (file.isFile() && file.exists()) { InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { String[] result = getNamePhone(lineTxt); System.out.println(lineTxt); content.append(result[0] + "----" + result[1]); content.append("\r\n");// txt换行 } read.close(); } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } return content.toString(); }
2. Name and phone number separation module:
public static String[] getNamePhone(String str) { String[] result = new String[2]; int index = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) >= '0' && str.charAt(i) <= '9') { index = i; break; } } result[0] = str.substring(0, index); result[1] = str.substring(index); return result; }
3. File writing module:
public static void printFile(String content) { BufferedWriter bw = null; try { File file = new File("D:/filename.txt"); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); bw = new BufferedWriter(fw); bw.write(content); bw.close(); } catch (IOException e) { e.printStackTrace(); } }
Through these three modules, you can realize the reading operation of the file, then process the information, and finally add the processed information to the new file.
What needs to be noted here is: the encoding format of the project must be written as utf-8, otherwise garbled characters will appear.
#The file reading and writing operations are completed here. Isn’t it particularly simple and convenient?
Thank you everyone for reading, I hope you will benefit a lot.
This article is reproduced from: https://blog.csdn.net/linzhiqiang0316/article/details/71744340
Recommended tutorial: "java tutorial"
The above is the detailed content of Java's reading and writing operations on files (detailed explanation with pictures and texts). For more information, please follow other related articles on the PHP Chinese website!