日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/9/15 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);

}

}

本回答由提問者推薦

已贊過

已踩過<

你對這個回答的評價是?

評論

收起

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的java data是什么文件_如何用java实现 读取一个data类型文件 并显示出来(随便选择一种类型txt或者word)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。