导航:首页 > 编程语言 > 文件预览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实现相关的资料

热点内容
什么app买吃的东西是批发价的 浏览:419
漏斗存钱罐解压玩具 浏览:783
夸克可以在线解压文件吗 浏览:518
大乐透投注费用算法 浏览:209
程序员前3到5年后的建议 浏览:130
共享电动车用什么app找 浏览:723
cpu具有编译功能吗 浏览:749
我的世界服务器怎么获得拒绝方块 浏览:923
手机加密密码去哪里能找到 浏览:169
什么特效相机app好玩 浏览:952
凯叔命令词 浏览:85
制作云服务器怎么转发数据 浏览:721
文件预览java实现 浏览:984
青岛少儿编程 浏览:399
蜘蛛5音箱安卓软件怎么用 浏览:587
前公司源码可以用吗 浏览:126
单片机初始化程序编程 浏览:196
app流失率指什么 浏览:805
抗震等级梁的加密 浏览:919
液压机解压教学视频 浏览:991