
正文
java浏览文件代码 java文件如何在浏览器运行
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java代码怎么实现打开文件(.xls .doc .txt)
java有个类Desktop(java.awt.Desktop),可以满足你的需求,比如下面的代码:
try {
Desktop.getDesktop().open(new File("D:\\1.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
路径为本地磁盘的全路径,就可以打开D盘下的1.txt,希望能帮到你。
相关问答
Q1: 一个浏览上传多个文件的java代码
在Java中可以用struts2实现多个文件同时上传代码,功能代码如下:
1、upload.jsp页面(选择上传文件)
form action="upload.action" name="uploadForm" method="post" enctype="multipart/form-data"
文件标题:input type="text" name="title"/br/
选择文件-:input type="file" name="upload"/br/
选择文件二:input type="file" name="upload"/br/
选择文件三:input type="file" name="upload"/br/
input type="submit" value="upload"/
/form
2、action代码如下:
//对应的Action依次遍历所有文件域,然后生成对应的输入文件流,输出文件流在指定的服务器保存路径中添加对应的输出文件流保存文件。同时动态指定服务器上文件的保存路径。
package com.inspur.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String title;
private File[] upload;
private String[] uploadFileName;
private String[] uploadContentType;
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String upload()throws Exception{
File[] files=this.getUpload();
for(int i=0;ifiles.length;i++){
FileOutputStream fos=new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName()[i]);
byte[] buffer=new byte[1024];
FileInputStream fis=new FileInputStream(files[i]);
int len=0;
while((len=fis.read(buffer))0){
fos.write(buffer,0,len);
}
}
return SUCCESS;
}
}
3、success.jsp页面代码如下(上传成功界面显示所有上传的图片)
文件标题:s:property value="title"/br/
第一个图片:img alt="first" src="s:property value="'upload/'+uploadFileName[0]"/"/br/
第二个图片:img alt="second" src="s:property value="'upload/'+uploadFileName[1]"/"/br/
Q2: java读取文本文件代码
java读取文本文件java浏览文件代码的方法有很多 这个例子主要介绍最简单 最常用java浏览文件代码的BufferedReader类 完整例子如下 package net chinaunix blog hzm text;import java io BufferedReader;import java io FileReader;import java io IOException;public class ReadFile {private String path;public ReadFile(String filePath){path = filePath;}public String[] openFile() throws IOException{FileReader fr = new FileReader(path) BufferedReader textReader = new BufferedReader(fr) String[] textData = new String[readLines()];int i;for(i= ; i readLines() i++){textData[i] = textReader readLine() }textReader close() return textData;}int readLines() throws IOException{FileReader fileToRead = new FileReader(path) BufferedReader bf = new BufferedReader(fileToRead) int numberOfLines = ;@SuppressWarnings( unused )String oneLine;while((oneLine = bf readLine()) != null){numberOfLines++;}bf close() return numberOfLines;}}package net chinaunix blog hzm text;import java io IOException;public class FileData {public static void main(String[] args) throws IOException{String filePath = C:/text txt ;try{ReadFile reader = new ReadFile(filePath) String[] content = reader openFile() int i;for(i= ;icontent length;i++){System out println(content[i]) }}catch(IOException e){System out println( 异常信息 + e getMessage()) }}}java io BufferedReaderThe buffer size may be specified or the default size may be used The default is large enough for most purposes In general each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly such as FileReaders and InputStreamReaders For example BufferedReader in = new BufferedReader(new FileReader( foo in )) will buffer the input from the specified file Without buffering each invocation of read() or readLine() could cause bytes to be read from the file converted into characters and then returned which can be very inefficient Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader java io FileReaderFileReader is meant for reading streams of characters For reading streams of raw bytes consider using a FileInputStream lishixinzhi/Article/program/Java/hx/201311/26249
Q3: java怎么返回前端.log文件浏览
在Java中,您可以使用以下步骤将.log文件返回给前端进行浏览:
1. 读取.log文件内容:使用Java的文件读取类(例如FileReader或BufferedReader)读取.log文件的内容。
2. 将文件内容转换为字符串:使用Java的字符串操作类(例如StringBuilder或StringBuffer)将文件内容转换为字符串。
3. 将字符串返回给前端:使用Java的Web框架(例如Spring MVC或Servlet)将字符串返回给前端。您可以将字符串作为响应体返回,或者将其存储在模型中并在视图中进行渲染。
4. 在前端进行浏览:在前端,您可以使用JavaScript或其他相关技术将返回的字符串显示为可浏览的.log文件。例如,您可以将字符串分割为行,并将每行显示为HTML元素(例如p或div)。
需要注意的是,如果.log文件非常大,您可能需要考虑分段读取文件内容,并将其分批返回给前端,以避免内存溢出等问题。
Q4: 怎样用JAVA打开一个已经写好的TXT文件?
JAVA打开一个已经写好的TXT文件代码如下:
(1):public File chooseFile(int chooseMode)
{
JFileChooser fileChooser;
File fileName;
fileChooser=new JFileChooser();
int returnVal=-1;
// fileFilter=new FileNameExtensionFilter("文本文档","txt");
switch(chooseMode)
{
case FILE_OPEN_CHOOSE:
returnVal=fileChooser.showOpenDialog(this);
break;
case FILE_SAVE_CHOOSE:
returnVal=fileChooser.showSaveDialog(this);
break;
}
if(returnVal==fileChooser.APPROVE_OPTION)
fileName=fileChooser.getSelectedFile();
else fileName=null;
return fileName;
}
这个是通过打开文件对话框获取文件
public void showFile(File file) throws IOException
{
textArea.setText("");
BufferedReader br=new BufferedReader(new FileReader(file));
String text;
// buffer=new StringBuffer();
while((text=br.readLine())!=null)
// buffer.append(text+"\n");
textArea.append(text+"\n");
}
(2):import java.io.*;
import java.util.*;
public class readData
{
public static void main(String[] args)
{
BufferedReader in=new BufferedReader(new FileReader("你的文件名.txt"));
String s;
int i=0;
while((s=in.readLine())!=null)
{
StringTokenizer t=new StringTokenizer(s,"|");//"|"为分隔符
p[i].id=Integer.parseInt(t.nextToken());//将第一个记录赋给变量id
p[i].name=t.nextToken();//将第二个记录赋给变量name
p[i].age=Integer.parseInt(t.nextToken());//将第三个记录赋给变量age
p[i].sex=t.nextToken();//将第四个记录赋给变量sex
i++;
}
in.close();
}
}
//**********************************************************
假设赋值得变量结构如下:
class p
{
public int id;
public String name;
public int age;
public String sex;
}
关于java浏览文件代码和java文件如何在浏览器运行的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







