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

The method of Java file operation

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "Java file operation method", the editor shows you the operation process through the actual case, the operation method is simple and fast, practical, I hope this "Java file operation method" article can help you solve the problem.

Brief introduction

This procedure mainly uses FileInputStream and FileOutputStream to operate on files. Specifically, it includes opening the file through the relative path, reading the file in three ways, viewing the file properties, appending the file data and so on.

Effect picture:

Complete code:

Package Code.a;import java.io.*;public class FileInputStreamDemo {public static void main (String [] args) {/ / get the current directory; File f = new File ("."); System.out.print ("absolute path:" + f.getAbsolutePath () + "\ n"); while (true) {try {/ / enter the command System.out.print ("Please input your order:"); BufferedReader stdinBufferedReader; String str1 = null; stdinBufferedReader = new BufferedReader (new InputStreamReader (System.in)); str1 = stdinBufferedReader.readLine (); / / relative path to open the file File file2 = new File (".\\ src\\ Code\\ a\\ Exception.java"); FileInputStream fis2 = new FileInputStream (file2); perform different actions according to different commands / / read all data at once if (str1.equals ("read all data at once") {byte [] buf = new byte [(int) (file2.length ())]; fis2.read (buf); String str = new String (buf) System.out.print (str); System.out.print ("\ n");} / / chunk read else if (str1.equals ("chunk read")) {int n = 1024 Byte [] buf = new byte [n]; while ((count = fis2.read (buf))! =-1) {System.out.print (new String (buf,0,count));} System.out.print ("\ n") } / / read data verbatim else if (str1.equals ("read data verbatim") {for (int I = 0; I < file2.length (); iTunes +) {char ch = (char) (fis2.read ()) System.out.print (ch);} System.out.print ("\ n");} / / exit else if (str1.equals ("exit")) {System.out.print ("quit\ n") Break;} / / View file properties else if (str1.equals ("View File Properties")) {System.out.print ("If the file or catalog exists:" + file2.exists () + "\ n") System.out.print ("If is it a file:" + file2.isFile () + "\ n"); System.out.print ("If is it a catalog:" + file2.isDirectory () + "\ n"); System.out.print ("FileName:" + file2.getName () + "\ n") System.out.print ("absolute path:" + file2.getAbsolutePath () + "\ n"); System.out.print ("The last time that the file was changed:" + file2.lastModified () + "\ n"); System.out.print ("The size of the file:" + file2.length () + "bites\ n") } / / append data else if to the file (str1.equals ("file append data")) {FileOutputStream fos2 = new FileOutputStream (file2,true); System.out.println ("Please input the content:"); BufferedReader ContentReader String str2 = null; ContentReader = new BufferedReader (new InputStreamReader (System.in)); str2 = ContentReader.readLine (); fos2.write (str2.getBytes ()); fos2.close ();} / / close the stream object Fis2.close ();} / / handles exceptions Catch (FileNotFoundException fnfe) {System.out.print ("The file open unsuccessfully.");} catch (IOException ioe) {ioe.printStackTrace ();} this is the end of the introduction of "methods for manipulating Java files". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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