资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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) {

e.printStackTrace();

}

}

}

java代码中打开文件

如果你只想实现,就像双击了电脑某个文件

让系统用其它应用去打开这个文件的话

可以用这个:

java.awt.Desktop.getDesktop().open(file);

用java编写一个打开指定文件的程序

运行以下代码试试看。

public static void main(String[] args) {

Frame frame = new Frame("打开文件窗口");

frame.setLayout(new FlowLayout(FlowLayout.CENTER));

frame.setBounds(100, 200, 400, 300);

frame.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

final TextField txtField = new TextField(50);

Button button = new Button("打开指定文件");

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String path = txtField.getText();

System.out.println(path);

if (path.length() == 0) {

return;

}

try {

Runtime.getRuntime().exec("explorer.exe /n, " + path);

} catch (IOException ex) {

ex.printStackTrace();

}

}

});

frame.add(txtField);

frame.add(button);

frame.setVisible(true);

}

怎样用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中如何打开文件
链接地址:http://cdkjz.cn/article/dsgceos.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220