java选择打开文件代码_java实现打开文件功能
Java打开指定文件
java打开文件夹使用方法:
String strTmp= "D:\abc\";
Runtime.getRuntime().exec("explorer.exe" + strTmp);
java读取文件使用方法:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class OpenFile {
public static void main(String args[]) {
try {
BufferedReader br = new BufferedReader(new FileReader("c://EmailSpider.java"));
String line = "";
while((line=br.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
用java编写一个打开指定文件的程序
运行以下代码试试看.
public static void main(String[] args) {
Frame frame = new Frame("打开文件窗口");
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
});
Button button = new Button("打开指定文件");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String path = txtField.getText();
System.out.println(path);
if (path.length() == 0) {
return;
Runtime.getRuntime().exec("explorer.exe /n, " + path);
} catch (IOException ex) {
ex.printStackTrace();
frame.add(txtField);
frame.add(button);
frame.setVisible(true);
java代码中打开文件
如果你只想实现,就像双击了电脑某个文件
让系统用其它应用去打开这个文件的话
可以用这个:
java.awt.Desktop.getDesktop().open(file);
如何用JAVA选择文件后直接打开该文件?
该段代码功能只是根据文件的类型获取文件的路径,如果要想打开文件就需要一个完整的工具,你需要知道该文件的存储格式,数据结构等,并针对该文件采取有效的算法.
目前你的功能只是完成了"打开"功能中的选择阶段.
怎样用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);
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");
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选择打开文件代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!