import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;
public class ReadFromFile {
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
private static DataInputStream dis=null;
private static DataOutputStream dos=null;
private static List<MrBean> list=new ArrayList<MrBean>();
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
int i=0;
/*try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.println(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}*/
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[20];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
//System.out.println(Integer.parseInt(new String(tempbytes, 0, 4)));
System.out.write(tempbytes, 0, 4);
System.out.write(tempbytes, 4, 2);
System.out.write(tempbytes, 6, 2);
System.out.write(tempbytes, 8, 2);
System.out.write(tempbytes, 10, 2);
System.out.write(tempbytes, 12, 1);
System.out.write(tempbytes, 13, 1);
System.out.write(tempbytes, 14, 1);
System.out.write(tempbytes, 15, 1);
System.out.write(tempbytes, 16, 4);
//System.out.println(tempbytes.toString());
System.out.write(tempbytes, 0, byteread);
System.out.print("\n");
//System.out.println(new String(tempbytes, 0, byteread));
i++;
if(i==10)
break;
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
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下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}*/
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
// 一次读多个字符
char[] tempchars = new char[20];
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) {
}
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
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) {
}
}
}
}
/**
* 显示输入流中还剩的字节数
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readMethod2(String fileName)
{
try
{
FileInputStream stream=new FileInputStream(fileName);
int c;
while((c=stream.read())!=-1)
{
System.out.println(c);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
/**读取二进制文件**/
public static void readBinaryStream(String fileName){
//System.out.println("123");
//int i=0;
//File file = new File(fileName);
try{
dis=new DataInputStream(new FileInputStream(new File(fileName)));
//System.out.println(dis.available());
if(dis!=null){
while(dis.available()>0){
//System.out.println("456");
//System.out.println(dis.available());
MrBean mrBean=new MrBean();
//System.out.print(dis.readInt()+" ");
mrBean.setTIME_STAMP(dis.readInt());
//System.out.print((int)dis.readChar()+" ");
mrBean.setTRX_ID((int)dis.readChar());
//System.out.print((int)dis.readChar()+" ");
mrBean.setLAC((int)dis.readChar());
//System.out.print((int)dis.readChar()+" ");
mrBean.setCI((int)dis.readChar());
//System.out.print((int)dis.readChar()+" ");
mrBean.setTEI((int)dis.readChar());
//System.out.print(dis.readByte()+" ");
mrBean.setVALUE_1(dis.readByte());
//System.out.print(dis.readByte()+" ");
mrBean.setVALUE_2(dis.readByte());
//System.out.print(dis.readByte()+" ");
mrBean.setVALUE_3(dis.readByte());
//System.out.print(dis.readByte()+" ");
mrBean.setVALUE_4(dis.readByte());
//System.out.print(dis.readInt()+" ");
mrBean.setCOUNTS(dis.readInt());
//System.out.println(dis.read(m_datapadding));
list.add(mrBean);
//System.out.println("");
/* i++;
if(i==100)
break;*/
}
}
}catch(Exception e){
e.printStackTrace();
}
}
/**写入二进制文件**/
public static void writeBinaryStream(String fileName,List<MrBean> list){
try{
dos=new DataOutputStream(new FileOutputStream(new File(fileName)));
if(dos!=null){
for(int i=0;i<list.size();i++){
dos.writeInt(list.get(i).getTIME_STAMP());
dos.writeChar(list.get(i).getTRX_ID());
dos.writeChar(list.get(i).getLAC());
dos.writeChar(list.get(i).getCI());
dos.writeChar(list.get(i).getTEI());
dos.writeByte(list.get(i).getVALUE_1());
dos.writeByte(list.get(i).getVALUE_2());
dos.writeByte(list.get(i).getVALUE_3());
dos.writeByte(list.get(i).getVALUE_4());
dos.writeInt(list.get(i).getCOUNTS());
//dos.write(m_datapadding);
}
dos.flush();
dos.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "E:/RLU_RLD.dat";
String outFileName = "E:/RLU_RLD_SORTED.DAT.dat";
//ReadFromFile.readMethod2(fileName);
//ReadFromFile.readFileByBytes(fileName);
//ReadFromFile.readFileByChars(fileName);
//ReadFromFile.readFileByLines(fileName);
//ReadFromFile.readFileByRandomAccess(fileName);
ReadFromFile.readBinaryStream(fileName);
QuickSort.quickSortByList(list, 0, list.size()-1);
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i).getTIME_STAMP()+" "+list.get(i).getTRX_ID()+" "+list.get(i).getLAC()+" "+list.get(i).getCI()+" "+list.get(i).getTEI()+" "+list.get(i).getVALUE_1()+" "+list.get(i).getVALUE_2()+" "+list.get(i).getVALUE_3()+" "+list.get(i).getVALUE_4()+" "+list.get(i).getCOUNTS());
}
ReadFromFile.writeBinaryStream(outFileName, list);
}
}