博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
什么是Java文件?
阅读量:2538 次
发布时间:2019-05-11

本文共 4473 字,大约阅读时间需要 14 分钟。

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 methodsimport java.io.File;//import the Exception class because it may raise //an exception when working with filesimport 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.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 filesimport 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.javaD:\Programs>java WriteFileFile 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 methodsimport java.io.File;//import the Exception class because it may // raise an exception when working with filesimport java.lang.Exception;// import the Scanner class to read file from userimport 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 ReadFileWe are going to write a file by using write()

翻译自:

转载地址:http://yfxzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-01 什么是微服务的注册中心
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-03CAP原理、常见面试题
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-06 服务注册和发现之Eureka Client搭建商品服务实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-07 Eureka服务注册中心配置控制台问题处理...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-01 常用的服务间调用方式讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-02 微服务调用方式之ribbon实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-03 高级篇幅之Ribbon负载均衡源码分析实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-05 微服务调用方式之feign 实战 订单调用商品服务...
查看>>