導航:首頁 > 編程語言 > 文件預覽java實現

文件預覽java實現

發布時間:2024-10-26 14:11:22

Ⅰ 如何用java實現 讀取一個data類型文件 並顯示出來(隨便選擇一種類型txt或者word)

參考下面的程序,基本上已經包含了文件讀取的所有方式,這也是我之前學習的一個小程序,希望對你有所幫助~~~~

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);
}

}

Ⅱ java web實現在線預覽word excel等文件,類似郵箱那種,非常感謝

Excel這部分可以用SpreadJS,這是一個純前端的控制項,用於在線Excel表格展示預覽操作。既然你是要實現在線預覽,肯定是要包含前端的,SpreadJS本身是純前端的,任何服務端語言都可以與之結合,所以java肯定也沒問題。

Word這塊可以找一找網上比較好的富文本工具,也可以實現word的在線預覽。

Ⅲ javaWeb開發中怎麼讓文件可以在線預覽,比如預覽doc,txt,ceb文件。

  1. 一般下載時能在線打開,我以前做過pdf的,貌似是本地軟體支持的,即在線打開只是調用本地的軟體。如pdf閱讀器類。

  2. 如果說能直接點擊文件查看的,以前我用過webOffice的控制項。就叫點聚webOffice,可以在線打開。

閱讀全文

與文件預覽java實現相關的資料

熱點內容
漏斗存錢罐解壓玩具 瀏覽:783
誇克可以在線解壓文件嗎 瀏覽:518
大樂透投注費用演算法 瀏覽:209
程序員前3到5年後的建議 瀏覽:130
共享電動車用什麼app找 瀏覽:723
cpu具有編譯功能嗎 瀏覽:749
我的世界伺服器怎麼獲得拒絕方塊 瀏覽:923
手機加密密碼去哪裡能找到 瀏覽:169
什麼特效相機app好玩 瀏覽:952
凱叔命令詞 瀏覽:85
製作雲伺服器怎麼轉發數據 瀏覽:721
文件預覽java實現 瀏覽:983
青島少兒編程 瀏覽:399
蜘蛛5音箱安卓軟體怎麼用 瀏覽:587
前公司源碼可以用嗎 瀏覽:125
單片機初始化程序編程 瀏覽:195
app流失率指什麼 瀏覽:805
抗震等級梁的加密 瀏覽:919
液壓機解壓教學視頻 瀏覽:991
fmm推薦演算法 瀏覽:849