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/01 Report--
This article mainly introduces java how to achieve simple document reading and writing, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Simple reading and writing of files
Package com.file;import java.io.*;/** * Created by elijahliu on 2017-2-11. * / public class ReadFile {public static void main (String [] args) {File file = new File ("new Hello.txt"); if (file.exists ()) {System.err.print ("exsit") Try (FileInputStream fis = new FileInputStream (file)) {/ / File input stream this is a byte stream InputStreamReader isr = new InputStreamReader (fis, "UTF-8"); / / inputstreamReader is a byte stream. When you convert a byte stream to a character stream, you need to develop an encoding method, otherwise it will garbled BufferedReader br = new BufferedReader (isr); / / character buffer String line While ((line = br.readLine ())! = null) {/ / here print System.out.println (line) if the contents of the buffer are not empty;} br.close (); / / finally close each thread isr.close (); fis.close ();} catch (FileNotFoundException e) {e.printStackTrace () } catch (IOException e) {e.printStackTrace ();}} File newfile = new File ("newtext.txt"); try {FileOutputStream fos = new FileOutputStream (newfile); / / here the file OutputStreamWriter osw = new OutputStreamWriter (fos, "UTF-8") is automatically created if the file does not exist; / / as with the read, here is the converted byte and character stream BufferedWriter bw = new BufferedWriter (osw) / / here is the write buffer bw.write ("awesome my brother"); / / write the string bw.close (); / / open here and then close osw.close (); fos.close (); System.out.println ("done");} catch (FileNotFoundException e) {e.printStackTrace () } catch (UnsupportedEncodingException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} Thank you for reading this article carefully. I hope the article "how to read and write documents in java" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.