⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileiostreamdemo.java

📁 JAVA编程思想源代码 值得一下 很难找的
💻 JAVA
字号:
package chapter10;

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

public class FileIOStreamDemo {

	public static void main(String[] args) {
		File f = new File("C://test.txt");
		String str = "This is the input.";
		byte[] buffer = new byte[str.length()];
		buffer = str.getBytes();
		try {
			if (!f.exists()) {
				f.createNewFile();
			}
			FileInputStream input1 = new FileInputStream(f);
			FileOutputStream output = new FileOutputStream(f);
			//输入数据到文件
			output.write(buffer);
			output.write(buffer, 5, buffer.length - 5);
			output.close();
			//从文件读取数据
			byte[] b1 = new byte[input1.available()];
			//从文件开头读取所有的字节
			System.out.println("从文件读取的数据流的字节数为:" + input1.available());
			input1.read(b1);
			String s1 = new String(b1);
			System.out.println(s1);
			System.out.println("数据流读取完毕后可读取的字节数为:" + input1.available());
			FileInputStream input2 = new FileInputStream(f);
			//跳过文件开头5个字节读取后面的字节
			input2.skip(5);
			System.out.println("跳过头五个字节,从文件读取的数据流的字节数为:" + input2.available());
			byte[] b2 = new byte[input2.available()];
			input2.read(b2);
			String s2 = new String(b2);
			System.out.println(s2);
			System.out.println("数据流读取完毕后可读取的字节数为:" + input2.available());
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -