In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how java can write and save txt files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The example code is as follows:
Package TEST;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;public class BufferedWriterDemo {public static void main (String [] args) throws IOException {write ("E:\\ 1.txt"); / / run the main method} public static void write (String path) throws IOException {/ / convert writes to streams in the form BufferedWriter bw = new BufferedWriter (new FileWriter (path)); / / write one line at a time String ss = "test data" Bw.write (ss); bw.newLine (); / / Line wrap / / close stream bw.close (); System.out.println (write successful);}}
Java creates the txt file and saves the content
Import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;public class txtExport {private static String path = "args /"; private static String filenameTemp;public static void main (String [] args) throws IOException {txtExport.creatTxtFile ("Hello"); txtExport.writeTxtFile ("Hello") } / * create file * * @ throws IOException*/public static boolean creatTxtFile (String name) throws IOException {boolean flag = false;filenameTemp = path + name + ".txt"; File filename = new File (filenameTemp); if (! filename.exists ()) {filename.createNewFile (); flag = true;} return flag } / * write file * * @ param newStr* new content * @ throws IOException*/public static boolean writeTxtFile (String newStr) throws IOException {/ / read the contents of the original file first, and then write boolean flag = false;String filein = newStr + "\ r\ n"; String temp = ""; FileInputStream fis = null;InputStreamReader isr = null;BufferedReader br = null;FileOutputStream fos = null;PrintWriter pw = null;try {/ / file path File file = new File (filenameTemp) / / read the file into the input stream fis = new FileInputStream (file); isr = new InputStreamReader (fis); br = new BufferedReader (isr); StringBuffer buf = new StringBuffer (); / / save the original contents of the file for (int j = 1; (temp = br.readLine ())! = null; jacks +) {buf = buf.append (temp) / / System.getProperty ("line.separator") / / the separator between lines is equivalent to "\ n" buf = buf.append (System.getProperty ("line.separator"));} buf.append (filein); fos = new FileOutputStream (file); pw = new PrintWriter (fos); pw.write (buf.toString (). ToCharArray ()); pw.flush (); flag = true;} catch (IOException E1) {/ / TODO automatically generates catch blocks throw e1 } finally {if (pw! = null) {pw.close ();} if (fos! = null) {fos.close ();} if (br! = null) {br.close ();} if (isr! = null) {isr.close ();} if (fis! = null) {fis.close ();}} return flag;}} Thank you for reading! This is the end of the article on "how to write and save txt files in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.