导航:首页 > 编程语言 > java打开指定文件

java打开指定文件

发布时间:2022-07-21 15:06:51

㈠ 用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在指定坐标打开指定exe文件

public static void main(String args[]) throws Exception
{
File file = new File("abc.html");
Runtime ce=Runtime.getRuntime();
System.out.println(file.getAbsolutePath());
ce.exec(file.getAbsolutePath());
}

像这样,想打开同目录下的abc.html,怎么不行呢?
可以直接输入使用,对于计算年龄工龄等非常有效。DAY

㈢ 怎么用Java实现打开文件(打开方法)

Process p = Runtime.getRuntime().exec("notepad");.

可以用java执行cmd命令的方式打开程序,比如上面是打开windows记事本的指令。如果你要打开其他文件,那就把notepad改成对应的文件名或程序名

㈣ java如何打开目录下特定文件

public class TestByteIO {
public static void main(String[] args) {
File f = new File(“c:\\temp.txt”); //创建文件对象
try {
// 通过文件对象创建文件输入流
FileInputStream filein = new FileInputStream(f);
//创建字节数组,用于接收从文件中读取的字节
byte buf[] = new byte[1024];
String instr = ""; //接收字节转化的字符串
int length = filein.read(buf);
instr = new String(buf,0,length);//将字节转化成字符串
System.out.println(instr);
filein.close(); //关闭输入流
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

㈤ java读取指定目录下的文件内容

publicclassReadFromFile{
/**
*以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
(StringfileName){
Filefile=newFile(fileName);
InputStreamin=null;
try{
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
//一次读一个字节
in=newFileInputStream(file);
inttempbyte;
while((tempbyte=in.read())!=-1){
System.out.write(tempbyte);
}
in.close();
}catch(IOExceptione){
e.printStackTrace();
return;
}
try{
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
//一次读多个字节
byte[]tempbytes=newbyte[100];
intbyteread=0;
in=newFileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
//读入多个字节到字节数组中,byteread为一次读入的字节数
while((byteread=in.read(tempbytes))!=-1){
System.out.write(tempbytes,0,byteread);
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(in!=null){
try{
in.close();
}catch(IOExceptione1){
}
}
}
}

/**
*以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
(StringfileName){
Filefile=newFile(fileName);
Readerreader=null;
try{
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
//一次读一个字符
reader=newInputStreamReader(newFileInputStream(file));
inttempchar;
while((tempchar=reader.read())!=-1){
//对于windows下, 这两个字符在一起时,表示一个换行。
//但如果这两个字符分开显示时,会换两次行。
//因此,屏蔽掉 ,或者屏蔽 。否则,将会多出很多空行。
if(((char)tempchar)!=' '){
System.out.print((char)tempchar);
}
}
reader.close();
}catch(Exceptione){
e.printStackTrace();
}
try{
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
//一次读多个字符
char[]tempchars=newchar[30];
intcharread=0;
reader=newInputStreamReader(newFileInputStream(fileName));
//读入多个字符到字符数组中,charread为一次读取字符数
while((charread=reader.read(tempchars))!=-1){
//同样屏蔽掉 不显示
if((charread==tempchars.length)
&&(tempchars[tempchars.length-1]!=' ')){
System.out.print(tempchars);
}else{
for(inti=0;i<charread;i++){
if(tempchars[i]==' '){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}

}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}

/**
*以行为单位读取文件,常用于读面向行的格式化文件
*/
(StringfileName){
Filefile=newFile(fileName);
BufferedReaderreader=null;
try{
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader=newBufferedReader(newFileReader(file));
StringtempString=null;
intline=1;
//一次读入一行,直到读入null为文件结束
while((tempString=reader.readLine())!=null){
//显示行号
System.out.println("line"+line+":"+tempString);
line++;
}
reader.close();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}

/**
*随机读取文件内容
*/
(StringfileName){
RandomAccessFilerandomFile=null;
try{
System.out.println("随机读取一段文件内容:");
//打开一个随机访问文件流,按只读方式
randomFile=newRandomAccessFile(fileName,"r");
//文件长度,字节数
longfileLength=randomFile.length();
//读文件的起始位置
intbeginIndex=(fileLength>4)?4:0;
//将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
intbyteread=0;
//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
//将一次读取的字节数赋给byteread
while((byteread=randomFile.read(bytes))!=-1){
System.out.write(bytes,0,byteread);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(randomFile!=null){
try{
randomFile.close();
}catch(IOExceptione1){
}
}
}
}

/**
*显示输入流中还剩的字节数
*/
(InputStreamin){
try{
System.out.println("当前字节输入流中的字节数为:"+in.available());
}catch(IOExceptione){
e.printStackTrace();
}
}

publicstaticvoidmain(String[]args){
StringfileName="C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}

㈥ java打开某个文件

常规文件用文件流打开读取:
class openfile
{
public static void main(String args[])
{
int temp;//定义一个临时整型变量,存储流数据
FileInputStream fis=new FileInputStream("文件完整路径");//文件输入流参数为文件名
FileOutputStream fos=new FileOutputStream(FileDescriptor.out);//输出流参数为默认显示器
while((temp=fis.read())!=-1)
fos.write(temp);
fis.close();
fos.close();//用完关闭流
}
}

㈦ 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代码实现打开指定的文件并显示文件中的内容

import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class practice
{ public static void main(String args[])
{ int b;
byte tom[]=new byte[25];
try{ File f=new File("Example.java");
FileInputStream in=new FileInputStream(f);
while((b=in.read(tom,0,25))!=-1)
{ String s=new String (tom,0,b);
System.out.print(s);
}
in.close();
}
catch(IOException e)
{ System.out.println("File read Error"+e);
}
}
}

㈨ 怎样用java打开指定文件

File file = new File("文件绝对路径");
Desktop.getDesktop().open(file);
即可调用系统的默认打开工具,打开这个文件

㈩ 怎么打开java文件

可以使用JRE
(Java
Runtime
Enviroment)打开:
STEP1:将jad.exe拷贝到JRE的bin目录下,如\Java\jre1.5.0_06\bin
STEP2:配置好你系统的JAVA环境,也就是在PATH里添加你的JRE的路径什么的...(这个还不懂??上GOOGLE去所搜一下,大把大把的)
STEP3:打开命令行,将目录切换到你想要破解的.class文件的目录.例如:我想要破解一个名为example.class的文件,它在e:\workspaces\里,那我就输入cd
e:\workspaces\让后回车.开始"脱"啦!:
编译单个JAVA语言的CLASS文件,在命令行输入:jad
example.class回车.
让后在你当前目录会生成一个Example.jad的文件!把它的后缀改为java就可以阅读了!(你可以忽略.class后缀)
选项
-s<后缀>允许你更改输出文件的后缀.
jad
-sjava
example.class
这条命令生成文件'example.java'.当你同时使用选项-o和-sjava时要小心了,因为Jad会意外的覆盖掉你的源文件.jad使用JAVA的CLASS文件名作为默认的输出文件名.举个例子,如果CLASS文件'example.class'包含了JAVA的CLASS'test'那么jad会优先生成'test.jad'.如果你想要指定一自己的输出文件名,请使用重定向:
jad
-p
example.class
>myexm.java选项-d允许你指定输出文件到另外一个文件夹,这个文件夹将默认创建在当前目录.举个例子:
jad
-o
-dtest
-sjava
*.class
(或者jad
-o
-d
test
-s
java
*.class,两者是一样的)
这条命令反编译所有.class文件在当前目录并且放置所有输出文件在目录'test'以.java为后缀名.如果你想反编译整个java类树,用如下命令:
jad
-o
-r
-sjava
-dsrc
tree/**/*.class
这条命令反编译所有.class文件位于'tree'以及子目录并且建立输出文件在'src'目录的相对子目录中依照类的包名.举个例子,如果文件'tree/a/b/c.class'包含类'c'来自包'a.b',那么数车文件的名字是'src/a/b/c.java'.万一你想要检查输出文件的精确度或者就是仅仅是好奇,这儿有个选项-a,它让jad使用JAVA虚拟机字节码作为注释添加在输出文件中.jad支持内部类和匿名类,当jad把通配符搭配到输入的文件名时,它自动的跳过匹配的内部类.在UNIX系统中jad跳过内部类如果有多于一个类在命令行中指定了.
JRE官方下载地址:
http://java.sun.com/javase/downloads/index.jsp

阅读全文

与java打开指定文件相关的资料

热点内容
堵车如何缓解压力 浏览:15
喜鹊快贷app怎么了 浏览:263
海龟编辑器积木编程怎么安装 浏览:185
程序员理发店生意怎么样 浏览:603
程序员罗技 浏览:180
软考初级程序员课程2021下载 浏览:491
杭州程序员奶奶 浏览:880
不听命令造成错误 浏览:981
kool系统源码 浏览:610
流氓app在哪里看 浏览:98
域名购买了怎么指向服务器 浏览:121
安卓手机如何让照片颜色反转 浏览:859
怎么下载卓睿安手机版 浏览:514
h3crange命令 浏览:468
php前景和python 浏览:338
php压缩图片内存大小 浏览:495
在哪里可以查看云服务器的信息 浏览:70
python读取非txt文件 浏览:799
艾莫迅用什么编程软件好 浏览:227
android文件存储读取 浏览:214