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

什么是Java文件?相关推荐

  1. java 文件压缩 解压_Java文件压缩与解压缩(一)

    package com.cn; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream ...

  2. Maven 手动添加第三方依赖包及编译打包和java命令行编译JAVA文件并使用jar命令打包...

    一,实例:新建了一个Maven项目,在eclipse中通过 build path –> configure path-.将依赖包添加到工程中后,eclipse不报错了.但是用Maven命令 mv ...

  3. 统计java文件中的代码行数

    统计Java代码行数工具类  --  CodeCounterUtil.java 统计指定目录下的java文件中代码行数  --  public static int  getCodeNumFromFo ...

  4. cmd库的导入Java,在cmd命令窗口导入第三方jar包来运行java文件

    在cmd命令窗口导入第三方jar包来运行java文件,以下测试都是基于window环境,Linux环境没有测试. 1.编译 使用命令javac -cp或者javac -classpath 本机测试:如 ...

  5. 另一个.java文件调用_java - 如何调用另一个类“写文件”的方法? - SO中文参考 - www.soinside.com...

    在我的Android应用程序,我想有一类处理所有"写入/读取到文本文件"的行动.所以,我根本就调用我的readUserFile.java文件我想的方法.但我的方法将不会在该文件中工 ...

  6. java9可执行jar_单个java文件打成可执行jar包

    1 概述 使用JDK自带的jar与java将单个java文件打成可执行jar包并运行. 当然也可以使用IDE完成,使用Maven只需要一个简单的package,但是单个文件嘛,没必要这么"凶 ...

  7. aidl生成java文件_Android Studio编写AIDL文件后如何实现自动编译生成

    Android Studio编写AIDL文件后如何实现自动编译生成 发布时间:2020-09-17 23:34:54 来源:脚本之家 阅读:111 作者:EdwardChu123 在目录src/mai ...

  8. Android Studio打开项目所有Java文件的import显示红色

    2019独角兽企业重金招聘Python工程师标准>>> 早上来到公司,打开Android Studio,然后在开源中国开始浏览新的资讯,无意间发现Java文件报错,大片大片的红色,很 ...

  9. 把java文件打包成.jar (jar命令详解)

    把java文件打包成.jar (jar命令详解) 先打开命令提示符(win2000或在运行框里执行cmd命令,win98为DOS提示符),输入jar Chelp,然后回车(如果你盘上已经有了jdk1. ...

  10. Eclipse中Java文件图标由实心J变成空心J的问题

    在eclipse中空心J的java文件,表示不被包含在项目中进行编译,而是当做资源存在项目中.例如: 当是单个文件为空心J的时候 1.右击该文件 – >BuildPath -->Inclu ...

最新文章

  1. 谷歌量子计算突破引爆学界,作者亲自回应质疑,国内专家点评
  2. Java基础-特殊数据类型:枚举
  3. ue4 怎么传递变量到另一个蓝图_资深建模教你放置UE4蓝图节点,所以你就不要偷懒啦,认真点学...
  4. php cgi远程控制,php-cgi如何使用(php cli模式执行php文件)
  5. 华为mate9云闪付功能_洗尽铅华:盘点华为那些深藏blue的手机
  6. cobbler 配置(转载)
  7. (转载)做好一个系统分析师、项目经理75条准则(一)
  8. 12-容器之间link
  9. 【MyBatis笔记】08-输出类型
  10. MySQL 5.7.9 免安装配置
  11. 进程控制(PCB,进程ID,进程状态,fork函数,文件共享)
  12. Xp正常及错误系统抓图汇编以及解决方案
  13. ASP程序部署在IIS上的步骤
  14. C语言实现String字符串及其函数
  15. c#语言猜数字游戏,使用C#实现猜数字游戏
  16. 串口拓展测试方法及步骤--信而泰TeleATT测试软件实操
  17. 最佳阵容怎么找不到服务器,最佳阵容新服最佳782服开服时间表_最佳阵容新区开服预告_第一手游网手游开服表...
  18. 3步轻松申请邮箱账号,申请163vip邮箱
  19. 十二个“一”的大五人格分析
  20. 实践是检验真理的唯一标准2 - 脱壳篇03

热门文章

  1. Bash脚本教程之行操作
  2. 掌握 React 与 React Native
  3. oracle数据库存大文本,Oracle大文本在ASP中存取问题的解决
  4. 【JS复习笔记】00 序
  5. push和unshift方法
  6. 关于html以及js相关格式验证的记录
  7. FormatJS – 让你的 Web 应用程序国际化
  8. 玩转异步 JS :async/await 简明教程(附视频下载)
  9. SQL Server2012 安装方法
  10. 从分布式锁角度理解Java的synchronized关键字