㈠ 用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