java data是什么文件_如何用java实现 读取一个data类型文件 并显示出来(随便选择一种类型txt或者word)...

展开全部

参考下面的程序32313133353236313431303231363533e4b893e5b19e31333264626635,基本上已经包含了文件读取的所有方式,这也是我之前学习的一个小程序,希望对你有所帮助~~~~

package com;

import java.io.BufferedReader;

public class TestFileInput {

private static final String FILENAME = "E:/test.txt";

private static final Logger logger = LoggerFactory.getLogger(TestFileInput.class);

public static void readByByte(String fileName) {

File file = new File(fileName);

InputStream in = null;

System.out.println("read file content by byte:");

try {

in = new FileInputStream(file);

int tempbyte = 0;

while ((tempbyte = in.read()) != -1) {

System.out.write(tempbyte);

}

in.close();

} catch (Exception e) {

logger.info("readByByte error!");

e.printStackTrace();

return;

}

try {

System.out.println("read by more byte:");

byte[] tempbytes = new byte[10];

int byteread = 0;

in = new FileInputStream(fileName);

while ((byteread = in.read(tempbytes)) != -1) {

System.out.write(tempbytes, 0, byteread);

}

} catch (Exception e) {

logger.info("read by more byte error!");

e.printStackTrace();

} finally {

if (in != null) {

try {

in.close();

} catch (Exception e) {

// do nothing

}

}

}

}

public static void readByChars(String fileName) {

File file = new File(fileName);

Reader reader = null;

System.out.println(" read by chars:");

try {

reader = new InputStreamReader(new FileInputStream(file));

int tempchar;

while ((tempchar = reader.read()) != -1) {

if (((char) tempchar) != '\r') {

System.out.println((char) tempchar);

}

}

reader.close();

} catch (Exception e) {

logger.info("read by chars error!");

e.printStackTrace();

}

System.out.println("read by more chars:");

try {

reader = new InputStreamReader(new FileInputStream(fileName));

char[] tempchars = new char[50];

int charread;

while ((charread = reader.read(tempchars)) != -1) {

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 e) {

logger.info("read by more chars error");

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public static void readByLines(String fileName) {

File file = new File(fileName);

BufferedReader reader = null;

System.out.println("read by line:");

try {

reader = new BufferedReader(new FileReader(file));

String tempString = null;

int line = 1;

while ((tempString = reader.readLine()) != null) {

System.out.println("line " + line + "is :" + tempString);

line++;

}

reader.close();

} catch (Exception e) {

logger.info("read by line error!");

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (Exception e) {

// do nothing

}

}

}

}

public static void readByRandomAccess(String fileName) {

RandomAccessFile file = null;

System.out.println("read by randomAccessFile:");

try {

file = new RandomAccessFile(fileName, "r");

long length = file.length();

int beginIndex = (length > 4) ? 4 : 0;

file.seek(beginIndex);

byte[] bytes = new byte[10];

int byteread = 0;

while ((byteread = file.read(bytes)) != -1) {

System.out.write(bytes, 0, byteread);

}

} catch (Exception e) {

logger.info("read by randomAccess error");

} finally {

if (file != null) {

try {

file.close();

} catch (Exception e) {

// do nothing

}

}

}

}

public static void main(String[] args) {

readByByte(FILENAME);

readByChars(FILENAME);

readByLines(FILENAME);

readByRandomAccess(FILENAME);

}

}

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值