「java打开本地文件」java 读取本地文件

博主:adminadmin 2022-11-23 21:55:09 38

今天给各位分享java打开本地文件的知识,其中也会对java 读取本地文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何使一个java程序,一执行,就打开本地某目录下的html文件?比如abc.html, 就像双击打开那样!

如何使一个java程序,一执行,就打开本地某目录下的html文件?比如abc.html, 就像双击打开那样! 15

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,怎么不行呢?

恩,你的命令不正确,怎么会打开呢?

试试我给你的代码吧

import java.io.File;

public class TestRuntime {

/**

* @param args

*/

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("cmd   /c   start  "+file.getAbsolutePath());

}

}

只要在你的同目录下有abc.html,就可以打开了

java代码中打开文件

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

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

可以用这个:

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

java怎样读取本地文件夹下的文件

  File file = new File("WebRoot\\test.html");

 BufferedReader bufferedReader = 

 new BufferedReader(new InputStreamReader(new FileInputStream(file)));         

    String row = null;

   StringBuffer sb = new StringBuffer();

    while ((row = bufferedReader.readLine()) != null) {

    System.out.println(row);

      sb.append(row);

     }

   bufferedReader .close();

如何用java打开一个本地文件

上代码

  String[] cmd = new String[]{

   "cmd.exe",

   "/c",

   // 第三个参数就是你要打开的文件路径

   "D:\\Work\\workspace\\GIFRecorder.rar"

  };

  Runtime.getRuntime().exec(cmd);

java 打开本地文件

你是要把里面的内容读出来?

package nb;

import java.io.*;

public class InputPic

{

public static void main(String args[]) throws Exception

{

String ppath=args[0];

File file=new File(ppath);

byte[] data=new byte[(int)file.length()];

FileInputStream fin=new FileInputStream(file);

fin.read(data);

fin.close();

}

}

用java 读取本地磁盘下的一个txt文件

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.IOException;

public class BufferedInputStreamDemo {

public static void main(String[] args) throws IOException {

// BufferedInputStream(InputStream in)

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(

"bos.txt"));

// 读取数据

// int by = 0;

// while ((by = bis.read()) != -1) {

// System.out.print((char) by);

// }

// System.out.println("---------");

byte[] bys = new byte[1024];

int len = 0;

while ((len = bis.read(bys)) != -1) {

System.out.print(new String(bys, 0, len));

}

// 释放资源

bis.close();

}

}

关于java打开本地文件和java 读取本地文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。