In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Java how to use Files.readLines() to process data in the text, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this to learn, I hope you can gain something.
Use Files.readLines() to process line data in text
During development, we encountered processing the data exported from the database to the file, and then writing the processed data back to the file. In this process, we used the Files.readLines() method.
/** * * @param file : existed file * @throws IOException */public void lineProcess(File file) throws IOException { Files.readLines(file, Charset.defaultCharset(), new LineProcessor() { File outFile = new File("outfile");//Processed data output file List lines = new ArrayList(); @Override public boolean processLine(String line) throws IOException { String newLine = ""; //line data format in file: name,age,address -> NAME,AGE,ADDRESS, String[] contents = line.split(","); for (int i=0;i= nChars) fill(); //read data here if (nextChar >= nChars) { /* EOF */ if (s != null && s.length() > 0) return s.toString(); else return null; } ......// Other}private void fill() throws IOException {.../ int n; do { n = in.read (cb, dst, cb.length - dst); //substantial} while (n == 0); if (n > 0) { nChars = dst + n; nextChar = dst; } }
From the above, readLine() calls read(char[] cbuf, int off, int len) to read the data, and then processes the data according to "/r" or "/n".
The Java I/O book also states:
public String readLine() throws IOException
This method returns a string that contains a line of text from a text file. /r, /n, and /r/n are assumed to be line breaks and are not included in the returned string. This method is often used when reading user input from System.in, since most platforms only send the user's input to the running program after the user has typed a full line (that is, hit the Return key).
readLine() has the same problem with line ends that DataInputStream's readLine() method has; that is, the potential to hang on a lone carriage return that ends the stream . This problem is especially acute on networked connections, where readLine() should never be used.
Summary, use readLine() must pay attention to
Read in the data to note that there are/r or/n or/r/n
Block when there is no data, return null when the data flow is abnormal or broken
Avoid readLine() when working with data streams such as sockets to avoid blocking for a line feed/carriage return
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.