Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How java reads the txt file and outputs the result

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces java how to read the txt file and output the results, the article is very detailed, has a certain reference value, interested friends must read it!

1.java reads the specified txt file and parses

File format:

Code:

Package com.thinkgem.wlw.modules.midea;import java.io.*;import java.util.ArrayList;import java.util.List;/** * @ Author: zhouhe * @ Date: 8:48 * / public class Test {public static void main (String [] args) {/ / folder path String path = "D:\\ input.txt"; try {List scanListPath = readFile02 (path); / / System.out.println (scanListPath); for (int I = 0; I < scanListPath.size () String mytext +) {String mytext = scanListPath.get (I); / / replace all tabs mytext = mytext.replaceAll ("\ t", ","); System.out.println (mytext); / / each line is converted into a new array to determine what String [] strArr= mytext.split (",") is the parameter corresponding to the parameter value according to the subscript; / / Note that the delimiter needs to be translated into for (int m = 0; m < strArr.length). Break; case +) {/ / System.out.println (strArr [m]); switch (m) {case 0: System.out.println ("time:" + strArr [m]); break; case 1: System.out.println ("methane:" + strArr [m]); break; case 2: System.out.println ("total hydrocarbons:" + strArr [m]); break; case 3: System.out.println ("total non-methane hydrocarbons:" + strArr [m]); break Case 4: System.out.println ("ammonia:" + strArr [m]); break; case 5: System.out.println ("hydrogen sulfide:" + strArr [m]); break; case 6: System.out.println ("oxygen:" + strArr [m]); break; default: break;} catch (IOException e) {System.out.println ("abnormal, unreadable!") ;}} / * read a text line by line read * * @ param path * @ return * @ throws IOException * / public static List readFile02 (String path) throws IOException {/ / use a collection of strings to store paths in the text, or use the String [] array List list = new ArrayList (); FileInputStream fis = new FileInputStream (path) / / prevent path garbled if the utf-8 garbled changes the txt created in GBK eclipse to UTF-8, and the txt created on the computer uses GBK InputStreamReader isr = new InputStreamReader (fis, "UTF-8"); BufferedReader br = new BufferedReader (isr); String line = "" While ((line = br.readLine ())! = null) {/ / if the path in the t x t file does not contain-string here is a filter if (line.lastIndexOf ("- -") < 0) {list.add (line);}} br.close (); isr.close (); fis.close (); return list;}

2.java reads all the txt files under the specified folder and outputs the contents (I have 2 txt files under a folder here):

Code:

Package com.thinkgem.wlw.modules.midea;import java.io.*;/** * @ Author zhouhe * @ Date 13:10 on 2019-10-10 * / public class Test2 {/ * * create a new class and put the following code in it. Be careful to set basePath (the folder you want to read), and the read and write methods are written. You can deactivate it according to your needs * * / static String basePath= "D:\\ test"; / * find all the csv-compliant files under the folder * * @ param dir folder objects * / public static void findFile (File dir) throws IOException {File [] dirFiles = dir.listFiles (); for (File temp: dirFiles) {if (! temp.isFile ()) {findFile (temp) } / / find the specified file if (temp.isFile () & & temp.getAbsolutePath (). EndsWith (".txt")) {/ / get the file path, including the file name String filePath = temp.getAbsolutePath (); / / get the file name String fileName = temp.getName (); System.out.println (temp.isFile () + "" + temp.getAbsolutePath ()); readFileContent (temp)) } / * @ param file File object to read * @ return returns the contents of the file * * / public static String readFileContent (File file) throws IOException {FileReader fr = new FileReader (file); BufferedReader br = new BufferedReader (fr); StringBuffer sb = new StringBuffer (); while (br.ready ()) {/ / sb.append (br.readLine ()); System.out.println (br.readLine ()) } System.out.println (sb.toString ()); return sb.toString ();} / * * @ param file File object to write * @ param content File content to write * * / public static void writeFileContent (File file,String content) throws IOException {FileWriter fw = new FileWriter (file); fw.write (content); fw.flush (); fw.close () } public static void main (String [] args) {try {findFile (new File (basePath));} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}

These are all the contents of the article "how to read txt files and output results from java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report