什么是Java文件?

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.

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

We will study three things:

我們將研究三件事:

  • Creating a file

    建立檔案

  • Reading a file

    讀取文件

  • Writing a file

    寫文件

1) Creating a file

1)創建一個文件

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()方法創建文件,并且此方法的返回類型為Boolean,因此它將返回true或false。 成功創建文件時返回true,創建文件失敗時返回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 message
if (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.java
D:\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 safely
fw.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.java
D:\Programs>java WriteFile
File has been written successfully

3) Reading a file

3)讀取文件

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

通過使用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.java
D:\Programs>java ReadFile
We are going to write a file by using write()

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377109.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377109.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377109.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

繞過本地驗證提交HTML數據

我們在入侵一個網站,比如上傳或者自己定義提交的文件時,會在本地的代碼中遇到阻礙,,也就是過 濾,過濾有兩種,一種是在遠程服務器的腳本上進行的過濾,這段代碼是在服務器上運行后產生作用的,這種過 濾方式叫做遠程過濾;另一種是在我們的IE瀏覽器里執行的腳本過濾,就是說是在我們…

《dp補卡——343. 整數拆分、96. 不同的二叉搜索樹》

343. 整數拆分 1、確定dp數組以及下標含義。 dp[i]:分拆數字i,可以得到的最大的乘積 2、確定遞推公式: dp[i]最大乘積出處:從1遍歷j到i,j * dp[i-j] 與 j * (i-j)取最大值。( 拆分j的情況,在遍歷j的過程…

Adroid學習之 從源碼角度分析-禁止使用回退按鈕方案

有時候,不能讓用戶進行回退操作,如何處理? 查看返回鍵觸發了哪些方法。在打開程序后把這個方法禁止了。問題:程序在后臺駐留,這樣就會出現,其他時候也不能使用回退按鈕。如何處理,在onpase()時方…

騎士游歷問題問題_騎士步行問題

騎士游歷問題問題Problem Statement: 問題陳述: There is a chessboard of size NM and starting position (sx, sy) and destination position (dx,dy). You have to find out how many minimum numbers of moves a knight goes to that destination position? 有…

Android基礎之用Eclipse搭建Android開發環境和創建第一個Android項目(Windows平臺)...

一、搭建Android開發環境 準備工作:下載Eclipse、JDK、Android SDK、ADT插件 下載地址:Eclipse:http://www.eclipse.org/downloads/ JDK:http://www.oracle.com/technetwork/java/javase/downloads/jdk7u9-downloads-1859576.html Android SD…

《dp補卡——01背包問題》

目錄01背包[416. 分割等和子集](https://leetcode-cn.com/problems/partition-equal-subset-sum/)[1049. 最后一塊石頭的重量 II](https://leetcode-cn.com/problems/last-stone-weight-ii/)[494. 目標和](https://leetcode-cn.com/problems/target-sum/)01背包 1、dp數組以及…

用JavaScript往DIV動態添加內容

參考&#xff1a;http://zhidao.baidu.com/link?url6jSchyqPiEYCBoKdOmv52YHz9r7MTBms2pK1N6ptOX1kaR2eg320mlW1Sr6n36hpOeOadBxC2rWWGuhZPbms-K <div id"show"></div>要填充的數據為: 這是一個測試例子.jquery&#xff1a;$(function(){ var data …

《dp補卡——完全背包問題》

N件物品和一個最多能背重量為W的背包。第i件物品的重量為weight[i]&#xff0c;得到的價值是value[i]。每件物品都有無限個(可以放入背包多次)&#xff0c;求解將哪些物品裝入背包里物品價值總和最大。 01背包和完全背包唯一不同在于遍歷順序上。 01背包的核心代碼&#xff1a…

Java中的類型轉換

類型轉換 (Typecasting) Typecasting is a term which is introduced in all the language similar to java. Typecasting是一個用與Java類似的所有語言引入的術語。 When we assign primitive datatype to another datatype. 當我們將原始數據類型分配給另一個數據類型時。 I…

讓crash文件中的內存地址變成函數名稱,

假如程序員編譯了inhouse給測試。 如果在測試過程中出現奔潰現象&#xff0c;我想程序員一般會來看Device Log 也就是 crash文件 如果crash文件遇到如下的情況&#xff0c;在重要的地方看不到函數名稱。我想是一件很奔潰的事情。 1 Exception Type: EXC_BAD_ACCESS (SIGSEGV)2…

《dp補卡——多重背包》

多重背包簡介&#xff1a; 有N種物品和一個容量為V的背包。第i種物品最多有Mi件可用&#xff0c;每件耗費的空間為Ci&#xff0c;價值為Wi。求解將哪些物品裝入背包可使得這些物品耗費的空間總和不超過背包容量&#xff0c;且價值總和最大。 將Mi件攤開&#xff0c;就是一個01背…

kafka消息確認ack_什么是確認(ACK)? ACK代表什么?

kafka消息確認ackACK&#xff1a;致謝 (ACK: Acknowledgment) An acknowledgment (ACK) is a signal that is passed among the communicating processes, computers, or devices to indicate acknowledgment, or delivery of the message, as a component of a communications…

CocoaAsyncSocket 套接字

CocoaAsyncSocket 套接字 https://github.com/robbiehanson/CocoaAsyncSocket Asynchronous socket networking library for Mac and iOS 用于iOS以及Mac的異步套接字網絡庫。 TCP GCDAsyncSocket and AsyncSocket are TCP/IP socket networking libraries. Here are the key…

谷歌瀏覽器設置緩存方法

谷歌瀏覽器設置緩存方法&#xff1a; 1、在桌面Google Chrome快捷方式&#xff0c;目標&#xff1a;找到 C:\Users\Splendid\AppData\Local\…\Application\chrome.exe 在這后面加上-Disk-Cache-Dir”Z:\TEMP” 注意: -Disk前面有空格&#xff0c;”Z:\TEMP” 是文件存放在Z盤T…

《dp補卡——買賣股票問題》

目錄121. 買賣股票的最佳時機貪心dp思路滾動數組優化122. 買賣股票的最佳時機 II123. 買賣股票的最佳時機 III188. 買賣股票的最佳時機 IV309. 最佳買賣股票時機含冷凍期714. 買賣股票的最佳時機含手續費121. 買賣股票的最佳時機 貪心 取最左最小值&#xff0c;取最右最大值&…

oo0ooo0ooo0oo_OoO的完整形式是什么?

oo0ooo0ooo0ooOoO&#xff1a;外出 (OoO: Out of Office) OoO is an abbreviation of "Out of Office". OoO是“不在辦公室”的縮寫。 It is an expression, which is commonly used in the Gmail platform. It is written in the body or the subject of the email…

SP2010開發和VS2010專家食譜--第三章節--高級工作流(2)--為沙盒解決方案創建自定義活動...

盡管沙河解決方案功能有限&#xff0c;你仍然可以開發自定義活動&#xff0c;在SharePoint Designer中使用而不用改變web.config或添加.ACTION文件到根文件夾。 轉載于:https://www.cnblogs.com/crazygolf/p/3856795.html

sql where 1=1和 0=1 的作用

where 11; 這個條件始終為True&#xff0c;在不定數量查詢條件情況下&#xff0c;11可以很方便的規范語句。 一、不用where 11 在多條件查詢中的困擾 舉個例子&#xff0c;如果您做查詢頁面&#xff0c;并且&#xff0c;可查詢的選項有多個&#xff0c;同時&#xff0c;還讓用戶…

j@2ff4f00f_J4F的完整形式是什么?

j2ff4f00fJ4F&#xff1a;只是為了好玩 (J4F: Just For Fun) J4F is an abbreviation of "Just For Fun". J4F是“ Just For Fun”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Faceboo…

《dp補卡——子序列問題》

目錄300. 最長遞增子序列674. 最長連續遞增序列718. 最長重復子數組1143. 最長公共子序列53. 最大子序和392. 判斷子序列115. 不同的子序列583. 兩個字符串的刪除操作72. 編輯距離647. 回文子串 &#xff08;與 5.最長回文子串思路差不多&#xff09;516. 最長回文子序列300. 最…