Java教程

韩顺平Java38——IO流homework

本文主要是介绍韩顺平Java38——IO流homework,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.

 

 

package homework;

import java.io.*;

/**
 * @author 紫英
 * @version 1.0
 * @discription
 */
@SuppressWarnings("all")
public class Homework01 {
    public static void main(String[] args) throws IOException {
        String dirPath = "E:\\mytemp";
        File file = new File(dirPath);
        if (file.exists()) {
            System.out.println("目录已存在");
        } else {
            if (file.mkdir()) {
                System.out.println("目录创建成功!");
            } else {
                System.out.println("目录创建失败!");

            }
            System.out.println("目录不存在可以创建");
        }
        File file2 = new File(dirPath, "hello.txt");
        if (!file2.exists()) {
            file2.createNewFile();
            System.out.println("hello.txt创建成功");

        } else {
            System.out.println("文件已存在");
        }
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file2));
        bufferedWriter.write("hello,world!");
        bufferedWriter.close();//一定记得关!!!!!
        System.out.println("写入成功");

    }
}

 

2.

 

 

 

package homework;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author 紫英
 * @version 1.0
 * @discription
 */
public class Homework02 {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("e:\\a.txt"));
       String s = null;
       int i =0;
       while ((s=bufferedReader.readLine())!=null){
           System.out.println(++i +" "+ s);
       }
       bufferedReader.close();


    }
}

 

3.

 

这篇关于韩顺平Java38——IO流homework的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!