main函数:
import java.io.*;
import java.util.Vector;
import cn.com.test.byteio.ReadFromFile;
/*
* 拷贝文件1的内容到文件2中(readFileByByte()),文件可以是任何形式;
* 除了按字节读写外,其他都用TXT文件测试,只测试读,输出到console
*/
public class Byte_test {
public static void main(String[] args){
String filename = "F:\\jiegou.png";
String filecopy = "F:\\copy.png";
String filetxt = "F:\\111.txt";
WriteToFile wtf = new WriteToFile();
wtf.WriteByByte(filename, filecopy);
ReadFromFile.readFileByBytes(filetxt);
System.out.println(" ****************************** ");
ReadFromFile.readFileByChars(filetxt);
System.out.println(" ****************************** ");
ReadFromFile.readFileByLines(filetxt);
System.out.println(" ****************************** ");
ReadFromFile.readFileByRandomAccess(filetxt);
}
}
ReadFromFile类,实现读取文件:
package cn.com.test.bytetest;
import java.io.*;
public class ReadFromFile{
InputStream in = null;
public ReadFromFile(String filename){
in = this.getClass().getResourceAsStream(filename);
}
/*
* 获取文件(读)的剩余可用长度AvailableByte(String filename)
* return int
* @param filename
*/
@SuppressWarnings("finally")
public static int AvailableByte(String filename){
File file = new File(filename);
int filelength = 0;
try{
InputStream is = new FileInputStream(file);
filelength = is.available();
System.out.println("文件长度:"+filelength);
}catch (IOException e) {
e.printStackTrace();
}finally{
return filelength;
}
}
/*
* 按字节读取文件 ReadByByte(String filename)
* 一次可以读取多个字节多用于图片、视频等非文本文档
* @paramfilename
*/
public void readFileByByte(String filename){
try{
File file = new File(filename);
InputStream in = null;
in = new FileInputStream(file);
int count = 0,verdict = 0;
count = ReadFromFile.AvailableByte(filename);
byte[] sbyte = new byte[count];
while((verdict = in.read(sbyte)) != -1){
System.out.write(sbyte,0,count);
}
}catch (IOException e) {
e.printStackTrace();
}finally{
if (in != null){
try {
in.close();
} catch (IOException e1) {
}
}
}
System.out.println("*************");
}
/**
* 以字符为单位读取文件readFileByChars(String fileName),
* 常用于读文本,数字等类型的文件
* @param fileName 文件名
*/
public static void readFileByChars(String fileName){
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1){
//对于windows下,rn这两个字符在一起时,表示一个换行。
//但如果这两个字符分开显示时,会换两次行。
//因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。
if (((char)tempchar) != 'r'){
System.out.print((char)tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(" ");
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
//一次读多个字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
//读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars))!=-1){
//同样屏蔽掉r不显示
if ((charread == tempchars.length)&&(tempchars[tempchars.length-1] != 'r')){
System.out.print(tempchars);
}else{
for (int i=0; i<charread; i++){
if(tempchars[i] == 'r'){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
}finally {
if (reader != null){
try {
reader.close();
} catch (IOException e1) {
}
}
System.out.println(" ");
}
}
/**
* 以行为单位读取文件readFileByLines(String fileName),
* 常用于读面向行的格式化文件
* @param fileName
*/
public static void readFileByLines(String fileName){
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null){
//显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e1) {
}
}
System.out.println(" ");
}
}
/**
* 随机读取文件内容readFileByRandomAccess
* @param fileName
*/
public static void readFileByRandomAccess(String fileName){
RandomAccessFile randomFile = null;
try {
System.out.println("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
// 读文件的起始位置
int beginIndex = (fileLength > 4) ? 4 : 0;
//将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
//将一次读取的字节数赋给byteread
while ((byteread = randomFile.read(bytes)) != -1){
System.out.write(bytes, 0, byteread);
}
} catch (IOException e){
e.printStackTrace();
} finally {
if (randomFile != null){
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
}
WriteToFile 类 ,实现拷贝功能:
package cn.com.test.bytetest;
import java.io.*;
public class WriteToFile {
public OutputStream os = null;
public WriteToFile(){
}
public void WriteByByte(String filename1,String filename2){
File filei = new File(filename2);
File fileo = new File(filename1);
int ver = 0;
byte[] sbyte = new byte[1000];
try{
FileInputStream fis = new FileInputStream(fileo);
FileOutputStream fos = new FileOutputStream(filei);
while((ver = fis.read(sbyte)) != -1){
fos.write(sbyte, 0, ver);
System.out.println("传输一段,长度1000");
}
if(fis!=null)fis.close();
if(fos!=null)fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
由于多种方式写文件是重复性工作,没有涉及;
API