日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

什么是Java文件?

發(fā)布時(shí)間:2023/12/1 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 什么是Java文件? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Java文件 (Java files)

The file is a class of java.io package.

該文件是java.io包的類。

If we create a file then we need to remember one thing before creating a file. First, we need to check whether a file exists of the same name or not. If a file of the same name exists then we can’t create a file of same name else we can create a file of same.

如果創(chuàng)建文件,那么在創(chuàng)建文件之前我們需要記住一件事。 首先,我們需要檢查文件是否存在相同名稱。 如果存在同名文件,那么我們將無(wú)法創(chuàng)建同名文件,否則我們將創(chuàng)建同名文件。

We will study three things:

我們將研究三件事:

  • Creating a file

    建立檔案

  • Reading a file

    讀取文件

  • Writing a file

    寫文件

1) Creating a file

1)創(chuàng)建一個(gè)文件

To create a file by using createNewFile() method and the return type of this method is Boolean so it returns true or false. It returns true during successful creation of the file and It returns false during the creation of file failure.

要使用createNewFile()方法創(chuàng)建文件,并且此方法的返回類型為Boolean,因此它將返回true或false。 成功創(chuàng)建文件時(shí)返回true,創(chuàng)建文件失敗時(shí)返回false。

Example:

例:

// import the File class because we will use File class methods import java.io.File; //import the Exception class because it may raise //an exception when working with files import java.lang.Exception;public class CreateFile {public static void main(String[] args) {try {// Specify the path of file and we use double slashes // to escape '\' character sequence for windows otherwise // it will be considerable as url.File file = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");// createNewFile() returns true if file is successfully // created and then we will get the name of the file // using getName() method and return false if the file // is already exists then we will get the messageif (file.createNewFile()) {System.out.println("File created: " + file.getName());} else {System.out.println("File already exists of same name!! Please try to create from other name ");}} catch (Exception e) {System.out.println("An error occurred.");e.printStackTrace();}} }

Output

輸出量

D:\Programs>javac CreateFile.javaD:\Programs>java File created: myjava.txt

2) Writing a file

2)寫文件

To write a file by using write() method of FileWriter class.

使用FileWriter類的write()方法寫入文件。

Example:

例:

// import the FileWriter class because // we will use FileWriter class methods write() import java.io.FileWriter; //import the Exception class because it may raise // an exception when working with files import java.lang.Exception;public class WriteFile {public static void main(String[] args) {try {// Create an object of FileWriter class FileWriter fw = new FileWriter("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt ");// To write a file by using write() method fw.write("We are going to write a file by using write()");// After writing a file then we need to close safelyfw.close();//After successfully written of file then display a message for the user System.out.println("File has been written successfully");} catch (Exception e) {System.out.println("An error occurred");e.printStackTrace();}} }

Output

輸出量

D:\Programs>javac WriteFile.javaD:\Programs>java WriteFile File has been written successfully

3) Reading a file

3)讀取文件

To read a file by using nextLine() method of Scanner class.

通過(guò)使用Scanner類的nextLine()方法讀取文件。

Example:

例:

// import the File class because we will // use File class methods import java.io.File; //import the Exception class because it may // raise an exception when working with files import java.lang.Exception; // import the Scanner class to read file from user import java.util.Scanner;public class ReadFile {public static void main(String[] args) {try {File fr = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt ");Scanner sc = new Scanner(fr);while (sc.hasNextLine()) {String file_read = sc.nextLine();System.out.println(file_read);}sc.close();} catch (Exception e) {System.out.println("An error occurred.");e.printStackTrace();}} }

Output

輸出量

D:\Programs>javac ReadFile.javaD:\Programs>java ReadFile We are going to write a file by using write()

翻譯自: https://www.includehelp.com/java/what-are-java-files.aspx

總結(jié)

以上是生活随笔為你收集整理的什么是Java文件?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。