package com.test.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOUtils {
/**
* 读取指定文件内容,按照十六进制输出到控制台
* 并且每输出10个byte换行
* @param fileName
* @throws IOException
*/
public static void printHex(String fileName) throws IOException {
//把文件作为字节流进行读操作
FileInputStream in = new FileInputStream(fileName);
int b;
int i = 1;
while ((b = in.read()) != -1) {
if (b <= 0xf) {
System.out.print("0");
}
System.out.print(Integer.toHexString(b) + " ");
if (i % 10 == 0) {
System.out.println("");
}
i++;
}
in.close();
}
public static void printHexByByteArray(String fileName) throws IOException {
FileInputStream in = new FileInputStream(fileName);
byte[] buf = new byte[20*1024];
//如果字节数组够大,可以一次性读完
//从in中批量读取字节,放入到buf这个字节数组中,从第0个位置开始放,最多放buf.length个,返回的是读到的字节的个数
/* int bytes = in.read(buf, 0, buf.length);
int j = 1;
for(int i = 0;i < bytes; i++) {
if (buf[i] <= 0xf) {
System.out.print("0");
}
System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");
if (j % 10 == 0) {
System.out.println("");
}
j++;
} */
//如果字节数组不够大,不能一次性读完
int bytes = 0;
int j = 1;
while ((bytes = in.read(buf, 0, buf.length)) != -1) {
for (int i = 0; i <bytes; i++) {
if (buf[i] <= 0xf) {
System.out.print("0");
}
System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");
if (j % 10 == 0) {
System.out.println("");
}
j++;
}
}
}
}
package com.test.io;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputDemo {
public static void main(String[] args) throws IOException {
//如果该文件不存在,则直接创建,如果存在,删除后创建。(如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。)
FileOutputStream out = new FileOutputStream("F:\\javaio\\out.dat");
out.write('A');//写入了‘A'的低八位(一次只写入一个字节)
int a = 10;
out.write(a >>> 24);
out.write(a >>> 16);
out.write(a >>> 8);
out.write(a);
byte[] b = "10".getBytes();
out.write(b);
out.close();
IOUtils.printHex("F:\\javaio\\out.dat");
}
}
package com.test.io;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputDemo {
public static void main(String[] args) throws IOException {
String file = "F:\\javaio\\b.txt";
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
dos.writeInt(10);
dos.writeInt(-10);
dos.writeLong(10l);
dos.writeDouble(10.5);
dos.writeUTF("你好");
dos.writeChars("中国");
dos.close();
IOUtils.printHex(file);
}
}
00 00 00 0a ff ff ff f6 00 00 00 00 00 00 00 0a 40 25 00 00 00 00 00 00 00 06 e4 bd a0 e5 a5 bd 4e 2d 56 fd
package com.test.io;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class DataInputDemo {
public static void main(String[] args) throws IOException {
String file = "F:\\javaio\\b.txt";
DataInputStream dis = new DataInputStream(new FileInputStream(file));
int i = dis.readInt();
System.out.println(i);
i = dis.readInt();
System.out.println(i);
long l = dis.readLong();
System.out.println(l);
double d = dis.readDouble();
System.out.println(d);
String s = dis.readUTF();
System.out.println(s);
dis.close();
}
}
10 -10 10 10.5 你好
package com.test.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOUtils {/**
* 拷贝文件,字节批量读取
* @param srcFile 源文件
* @param destFile 目标文件
* @throws IOException
*/
public static void copyFile(File srcFile, File destFile) throws IOException {
if (!srcFile.exists()) {
throw new IllegalArgumentException("文件" + srcFile + "不存在");
}
if (!srcFile.isFile()) {
throw new IllegalArgumentException(srcFile + "不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[10*1024];
int b;
while ((b = in.read(buf, 0, buf.length)) != -1) {
out.write(buf,0,b);
out.flush();//最好加上,刷新此输出流并强制写出所有缓冲的输出字节。
}
in.close();
out.close();
}
/**
* 拷贝文件,利用带缓冲的字节流
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFileByBuffer(File srcFile, File destFile) throws IOException {
if (!srcFile.exists()) {
throw new IllegalArgumentException("文件" + srcFile + "不存在");
}
if (!srcFile.isFile()) {
throw new IllegalArgumentException(srcFile + "不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(out);
int c;
while ((c = bis.read()) != -1) {
bos.write(c);
bos.flush();
}
bis.close();
bos.close();
}
/**
* 拷贝文件,通过单字节读取
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFileByByte(File srcFile, File destFile) throws IOException {
if (!srcFile.exists()) {
throw new IllegalArgumentException("文件" + srcFile + "不存在");
}
if (!srcFile.isFile()) {
throw new IllegalArgumentException(srcFile + "不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
int c;
while ((c = in.read()) != -1) {
out.write(c);
out.flush();
}
in.close();
out.close();
}
}
package com.test.io;
import java.io.File;
import java.io.IOException;
public class IOUtilsTest {
public static void main(String[] args) {
//IOUtils.printHex("D:\\javaProgram\\Hello.java");
try {
long start = System.currentTimeMillis();
//IOUtils.copyFile(new File("F:\\javaio\\1.mp3"), new File("F:\\javaio\\2.mp3"));//211ms
//IOUtils.copyFileByBuffer(new File("F:\\javaio\\1.mp3"), new File("F:\\javaio\\3.mp3"));//18583ms
IOUtils.copyFileByByte(new File("F:\\javaio\\1.mp3"), new File("F:\\javaio\\4.mp3"));//37822ms
long end = System.currentTimeMillis();
System.out.println(end - start);
} catch (IOException e) {
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有