java File流使用
2016-07-11 09:25
609 查看
package com.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInfo {
/**
* @author zm
* @param fileName 文件名
* @throws IOException
*/
public static void printHex(String fileName) throws IOException{
FileInputStream fis=new FileInputStream(fileName);
//文件名
int b;
int i=1;
//如果等于-1结束读取
while ((b=fis.read())!=-1) {
if(b<=0xf){
System.out.print("0");
}
System.out.print(Integer.toHexString(b)+" ");
if(i++%10==0){
System.out.println();
}
}
//关闭流
fis.close();
}
/**
* 对于大文件效率低
* @param fileName
* @throws IOException
*/
public static void printHexByByte(String fileName) throws IOException{
FileInputStream fis=new FileInputStream(fileName);
byte[] bytes=new byte[1024*8];
int byte2=0;
while ((byte2=fis.read(bytes,0,bytes.length))!=-1) {
for (int i = 0; i < bytes.length; i++) {
System.out.print(Integer.toHexString(bytes[i]& 0xff)+" ");
if(i++%10==0){
System.out.println();
}
//byte 8位 int 32 位 &0ff将高位24清零
}
}
}
}
TestFile类
package com.test;
import java.io.IOException;
import com.file.FileInfo;
public class TestFile {
/**
* @author zm
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// FileInfo.printHex("C://test.txt");
long start=System.currentTimeMillis();
//开始事件
System.out.println();
FileInfo.printHexByByte("C://test.txt");
long end=System.currentTimeMillis();
System.out.println(end-start);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInfo {
/**
* @author zm
* @param fileName 文件名
* @throws IOException
*/
public static void printHex(String fileName) throws IOException{
FileInputStream fis=new FileInputStream(fileName);
//文件名
int b;
int i=1;
//如果等于-1结束读取
while ((b=fis.read())!=-1) {
if(b<=0xf){
System.out.print("0");
}
System.out.print(Integer.toHexString(b)+" ");
if(i++%10==0){
System.out.println();
}
}
//关闭流
fis.close();
}
/**
* 对于大文件效率低
* @param fileName
* @throws IOException
*/
public static void printHexByByte(String fileName) throws IOException{
FileInputStream fis=new FileInputStream(fileName);
byte[] bytes=new byte[1024*8];
int byte2=0;
while ((byte2=fis.read(bytes,0,bytes.length))!=-1) {
for (int i = 0; i < bytes.length; i++) {
System.out.print(Integer.toHexString(bytes[i]& 0xff)+" ");
if(i++%10==0){
System.out.println();
}
//byte 8位 int 32 位 &0ff将高位24清零
}
}
}
}
TestFile类
package com.test;
import java.io.IOException;
import com.file.FileInfo;
public class TestFile {
/**
* @author zm
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// FileInfo.printHex("C://test.txt");
long start=System.currentTimeMillis();
//开始事件
System.out.println();
FileInfo.printHexByByte("C://test.txt");
long end=System.currentTimeMillis();
System.out.println(end-start);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序