Java Text-IO基础

File Class

public class TestFileClass {
  public static void main(String[] args) {
    java.io.File file = new java.io.File("image/us.gif");
    System.out.println("Does it exist? " + file.exists());
    System.out.println("Can it be read? " + file.canRead());
    System.out.println("Can it be written? " + file.canWrite());
    System.out.println("Is it a directory? " + file.isDirectory());
    System.out.println("Is it a file? " + file.isFile());
    System.out.println("Is it absolute? " + file.isAbsolute());
    System.out.println("Is it hidden? " + file.isHidden());
    System.out.println("Absolute path is " +
      file.getAbsolutePath());
    System.out.println("Last modified on " +
      new java.util.Date(file.lastModified()));
  }
}

Text-IO

This section introduces how to read/write strings and numeric values from/to a text file using the Scanner and PrintWriter classes.

PrintWriter

import java.io.*;


public class WriteData {
    public static void main(String[] args) throws Exception{
        File file = new File("scores.txt");
        if(file.exists()){
            System.out.println("File already exists");
            System.exit(0);
        }

        //Create a file
        PrintWriter output = new PrintWriter(file);

        //Write formatted out put to the file
        output.print("John T smith ");
        output.print(90);
        output.print(" Eric K Jones ");
        output.print(85);

        //Close the file
        output.close();
    }
}

Scanner

import java.util.*;
import java.io.*;

public class ReadData {

    public static void main(String[] args) throws Exception{
        // Create a File instance
        File file = new File("scores.txt");

        //Create a Scanner for the file
        Scanner input = new Scanner(file);

        //Read data from a file
        while(input.hasNext()){
            String firstname = input.next();
            String mi = input.next();
            String lastname = input.next();
            int score = input.nextInt();
            System.out.println(firstname + " " + mi + " " + lastname + " " + score);
        }

        //Close the file
        input.close();
    }

}

Example

import java.io.*;
import java.util.*;

public class ReplaceText {
  public static void main(String[] args) throws Exception {
    // Check command line parameter usage
    if (args.length != 4) {
      System.out.println(
        "Usage: java ReplaceText sourceFile targetFile oldStr newStr");
      System.exit(0);
    }

    // Check if source file exists
    File sourceFile = new File(args[0]);
    if (!sourceFile.exists()) {
       System.out.println("Source file " + args[0] + " does not exist");
       System.exit(0);
    }

    // Check if target file exists
    File targetFile = new File(args[1]);
    if (targetFile.exists()) {
      System.out.println("Target file " + args[1] + " already exists");
      System.exit(0);
    }

    // Create input and output files
    Scanner input = new Scanner(sourceFile);
    PrintWriter output = new PrintWriter(targetFile);

    while (input.hasNext()) {
      String s1 = input.nextLine();
      String s2 = s1.replaceAll(args[2], args[3]);
      output.println(s2);
    }

    input.close(); 
    output.close();
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值