java中io与file的简单了解

java中输入输出流的简单操作:


package com.ifly.classpractice.day7_30.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class StreamDemo {

	public static void main(String[] args) throws Exception {

		writeFile1();
		// writeFile2();

		// readTextFile();
		// readTextFile2();
	}

	private static void readTextFile2() throws Exception {
		FileInputStream in = new FileInputStream("D:/text2.txt");
		// System.out.println(in.available());
		byte[] buffer = new byte[in.available()];
		in.read(buffer);
		// 将得到的字节数组转化为String
		System.out.println(new String(buffer));
		in.close(); // 关闭输入流

	}

	private static void readTextFile() throws Exception {
		FileInputStream in = new FileInputStream("D:/text2.txt");

		byte[] buffer = new byte[100];
		while (in.read(buffer, 0, buffer.length) != -1) {
			System.out.println(new String(buffer));
			buffer = new byte[100];
		}
		in.close();
	}

	private static void writeFile2() throws Exception {
		// 往文件中追加
		FileOutputStream fos = new FileOutputStream("D:/text2.txt", true);

		PrintStream ps = new PrintStream(fos);

		for (int i = 0; i < 100; i++) {
			// ps.println("i have a dream!\r\n");
			ps.format("i have %s dream!\r\n", i);
			ps.write(new String("I love java").getBytes());
		}
		ps.flush();
		ps.close();
		fos.close();

	}

	private static void writeFile1() throws Exception {
		FileOutputStream fos = new FileOutputStream(new File("D:/text1.txt"));

		for (int i = 0; i < 100; i++) {
			byte[] bytes = String.format("I have %s dream!\r\n", i).getBytes();
			fos.write(bytes);
		}
		fos.flush();
		fos.close();

	}

}

package com.ifly.classpractice.day7_30.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;

public class FileReaderWriter {

	public static void main(String[] args) throws IOException {
		readerTextFile();
		// writeTextFile();

		// writeFile1();
		// writeFile2();

		// readTextFile();
		// readTextFile2();

	}

	/**
	 * 用字符方式写文件
	 * 
	 * @throws IOException
	 */
	private static void writeTextFile() throws IOException {
		// FileWriter out = new FileWriter(new File("D:/studentsInfo.txt"));
		FileWriter out = new FileWriter("D:/studentsInfo.txt", true);
		// for (int i = 0; i < 10; i++) {
		// out.write(String.format(" i have %s dream.\r\n", i));
		// }

		out.write("张三,1001,java3班\r\n");
		out.write("李四,1002,java3班\r\n");
		out.write("王五,1003,java3班\r\n");
		out.write("小六,1004,java3班\r\n");
		out.close();

	}

	/**
	 * 用字符方式读文件
	 * 
	 * @throws IOException
	 */
	private static void readerTextFile() throws IOException {
		FileReader in = new FileReader(new File("D:/studentsInfo.txt"));
		int ch = -1;
		while (-1 != (ch = in.read())) {
			System.out.print((char) ch);
		}
		in.close();
	}

	private static void readTextFile2() throws Exception {
		FileInputStream in = new FileInputStream("D:/text2.txt");
		// System.out.println(in.available());
		byte[] buffer = new byte[in.available()];
		in.read(buffer);
		// 将得到的字节数组转化为String
		System.out.println(new String(buffer));
		in.close(); // 关闭输入流

	}

	private static void readTextFile() throws Exception {
		FileInputStream in = new FileInputStream("D:/text2.txt");

		byte[] buffer = new byte[100];
		while (in.read(buffer, 0, buffer.length) != -1) {
			System.out.println(new String(buffer));
			buffer = new byte[100];
		}
		in.close();
	}

	private static void writeFile2() throws Exception {
		FileOutputStream fos = new FileOutputStream("D:/text2.txt", true);

		PrintStream ps = new PrintStream(fos);

		for (int i = 0; i < 100; i++) {
			// ps.println("i have a dream!\r\n");
			ps.format("i have %s dream!\r\n", i);
			ps.write(new String("I love java").getBytes());
		}
		ps.flush();
		ps.close();
		fos.close();

	}

	private static void writeFile1() throws Exception {
		FileOutputStream fos = new FileOutputStream(new File("D:/text1.txt"));

		for (int i = 0; i < 100; i++) {
			byte[] bytes = String.format("i have %s dream!\r\n", i).getBytes();
			fos.write(bytes);
		}
		fos.flush();
		fos.close();

	}

	private static void viewStudentInfoAll() {
		FileReader out = null;
		try {
			out = new FileReader(new File("D:/studentsResult.txt"));
			BufferedReader buffer = new BufferedReader(out);
			String str = new String();
			while ((str = buffer.readLine()) != null) {
				System.out.println(str);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}

java中文件路径

package com.ifly.classpractice.day7_30.io;

import static java.lang.System.out;

import java.io.File;

public class FileAndDirectory {

	public static void main(String[] args) throws Exception {
		out.println("当前目录"+System.getProperty("user.dir"));
		File directory = new File("");
		out.println(directory.exists());
		out.println(directory.getPath());
		out.println(directory.getAbsolutePath());
		out.println(directory.getCanonicalPath());
		
		File directory0 = new File("../src/com/ifly/classpractice/day7_30/io");
		out.println(directory0.exists());
		out.println(directory0.getPath());
		out.println(directory0.getAbsolutePath());
		out.println(directory0.getCanonicalPath());
		
		File directory1 = new File("./src/com/ifly/classpractice/day7_30/io");
		out.println(directory1.exists());
		out.println(directory1.getPath());
		out.println(directory1.getAbsolutePath());
		out.println(directory1.getCanonicalPath());
		
		
		
	}

}

当文件存在时删除,不存在时新建,并重命名文件名称

@Test
	public void newFileOrDeleteFile() throws IOException {
		File file = new File("E:/demo.txt");

		if (!file.exists()) {
			file.createNewFile();
		} else {
			file.delete();
		}

		System.out.println(file.exists());

		file.renameTo(new File("E:/demo.pdf")); // 重命名
	}

拷贝文件(将一个文件复制到另一个文件)

@Test
	public void testCopyFile() throws Exception {
		copyFile("E:/d.txt", "E:/e.txt"); // 拷贝文件
	}

	/**
	 * 拷贝文件
	 * 
	 * @param src
	 * @param dest
	 * @throws Exception
	 */
	public void copyFile(String src, String dest) throws Exception {
		FileInputStream fis = new FileInputStream(src);
		FileOutputStream fos = new FileOutputStream(dest);

		FileChannel fcIn = fis.getChannel();
		FileChannel fcOut = fos.getChannel();

		fcIn.transferTo(0, fcIn.size(), fcOut);
		fcOut.close();
		fcIn.close();
		fos.close();
		fis.close();
	}

列出目录中的所有文件

@Test
	public void testGetAllFile() throws Exception {
		getAllFile3("D:/"); // 列出目录中的所有文件
	}

	public void getAllFile3(String string) throws Exception {
		File[] files = new File(string).listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) {
				System.out.println(files[i].getCanonicalPath());
			} else {
				getAllFile3(files[i].getCanonicalPath());
			}
		}

	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

时间辜负了谁

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

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

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

打赏作者

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

抵扣说明:

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

余额充值