- public static void readFile(String fileName){
-
-
- File file = new File(fileName);
- if(file.exists()){
- try {
- FileInputStream in = new FileInputStream(file);
- DataInputStream dis=new DataInputStream(in);
-
- byte[] itemBuf = new byte[20];
-
- dis.read(itemBuf, 0, 8);
- String marketID =new String(itemBuf,0,8);
-
-
- dis.read(itemBuf, 0, 20);
- String marketName =new String(itemBuf,0,20);
-
-
- dis.read(itemBuf, 0, 8);
- String lastTradingDay = new String(itemBuf,0,8);
-
-
- dis.read(itemBuf, 0, 8);
- String curTradingDay = new String(itemBuf,0,8);
-
-
- dis.read(itemBuf, 0, 1);
- String marketStatus = new String(itemBuf,0,1);
-
-
- short tradePeriodNum = dis.readShort();
-
- System.out.println("市场代码:"+ marketID);
- System.out.println("市场名称:"+ marketName);
- System.out.println("上一交易日日期:"+ lastTradingDay);
- System.out.println("当前交易日日期:"+ curTradingDay);
- System.out.println("当前交易日日期:"+ curTradingDay);
- System.out.println("交易状态:"+ marketStatus);
- System.out.println("交易时段数:"+ tradePeriodNum);
-
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally{
-
- }
- }
- }
/**
* 随机读取文件内容
*/
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;
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
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) {
}
}
}
}