java创建文件和读取文件(Scanner和PrintWriter)

用Scanner来读取文件

1.Scanner类是在java.util.Scanner包中
2. Scanner类可以获取控制台的输入,当然也可用来读取文件。
3.使用Scanner对象中hasNext()方法来判断文件是否读取完毕
4.用nextLine(),nextInt()等方法来获取文本的信息

用PrintWriter来创建文件

1.java.io.PrintWriter类,该类可用来创建一个文件并向文本文件写入数据。
2.PrintWriter类可以理解为java中的文件输出,文件输入则是java.io.File。
3.提供了很多构造方法,介绍下面两种构造方法
这两种构造方法都是按照系统默认字符集对指定文件进行写操作
PrintWriter(String fileName)
PrintWriter(File file)

测试代码

import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Scanner;


public class test02 {
    public static void main(String[] args) throws IOException {
        //  文件对象创建
        File file = new File("..\\TEST\\1.txt");
        if (file.exists()) { // 检查1.txt是否存在,并打印绝对路径
            System.out.println("File already exists:"+file.getAbsolutePath());
            // Scanner类进行文件读取,new一个Scanner类,参数为文件的路径
            Scanner sc = new Scanner(file);
            while (sc.hasNext()) {
                //使用Scanner对象中hasNext()方法来判断文件是否读取完毕
                String temp = sc.nextLine();
                //nextLine(),nextInt()等方法来获取文本的信息
                System.out.println(temp);
            }
            //读取存在的文件完毕,则退出程序
            System.exit(1);
        }
        // 如果不存在则创建一个新文件
        try (PrintWriter output = new PrintWriter(file);) {
            //写入数据到指定的文件
            output.println("看,春风驾着五彩祥云姗姗而来,浑身散发着缕缕沁人的清香。");
            output.println("春风化雨,春雨戏风。他们嬉闹着,欢笑着,不知不觉荡涤了污浊的心灵。");
        
        }
    }
}

我的目录如下,目录存在1.txt文件,所以是读取
在这里插入图片描述

运行结果

输出1.txt的内容
在这里插入图片描述

创建文件

File file = new File(“…\TEST\2.txt”);创建文件对象时,如果是2.txt,文件不存在,则创建,目录和内容如下
在这里插入图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. PrintStream案例: ```java import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; public class PrintStreamDemo { public static void main(String[] args) { try { File file = new File("output.txt"); FileOutputStream fos = new FileOutputStream(file); PrintStream ps = new PrintStream(fos); ps.println("Hello, PrintStream!"); ps.println("This is a test."); ps.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 2. PrintWriter案例: ```java import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; public class PrintWriterDemo { public static void main(String[] args) { try { File file = new File("output.txt"); PrintWriter pw = new PrintWriter(file); pw.println("Hello, PrintWriter!"); pw.println("This is a test."); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 3. Scanner案例: ```java import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { try { File file = new File("input.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这些案例分别演示了PrintStream、PrintWriterScanner的用法。PrintStream和PrintWriter可以用来向文件或控制台输出内容,Scanner可以用来从文件或控制台读取内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yjg_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值