博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 文件 打开文件_如何用Java打开文件
阅读量:2531 次
发布时间:2019-05-11

本文共 2087 字,大约阅读时间需要 6 分钟。

java 文件 打开文件

Sometimes we have to open a file in java program. java.awt.Desktop can be used to open a file in java. Desktop implementation is platform dependent, so first, we should check if the operating system supports Desktop or not. This class looks for the associated application registered to the current platform to open a file.

有时我们必须在Java程序中打开文件。 java.awt.Desktop可用于在Java中打开文件。 桌面实施取决于平台,因此,首先,我们应该检查操作系统是否支持桌面。 此类查找在当前平台上注册的关联应用程序,以打开文件。

Java打开文件 (Java Open File)

Let’s have a look at the simple java open file program. If we try to open a file that doesn’t exist, it will throw
java.lang.IllegalArgumentException.

让我们看一下简单的Java打开文件程序。 如果我们尝试打开一个不存在的文件,它将抛出java.lang.IllegalArgumentException

Let’s see Desktop class example for java open file.

让我们看一下Java打开文件的Desktop类示例。

JavaOpenFile.java

JavaOpenFile.java

package com.journaldev.files;import java.awt.Desktop;import java.io.File;import java.io.IOException;public class JavaOpenFile {    public static void main(String[] args) throws IOException {        //text file, should be opening in default text editor        File file = new File("/Users/pankaj/source.txt");                //first check if Desktop is supported by Platform or not        if(!Desktop.isDesktopSupported()){            System.out.println("Desktop is not supported");            return;        }                Desktop desktop = Desktop.getDesktop();        if(file.exists()) desktop.open(file);                //let's try to open PDF file        file = new File("/Users/pankaj/java.pdf");        if(file.exists()) desktop.open(file);    }}

When you run the above program, the text file will be opened in the default text editor. Similarly, a PDF file will be opened in adobe acrobat reader.

当您运行上述程序时,该文本文件将在默认的文本编辑器中打开。 同样,将在Adobe Acrobat Reader中打开PDF文件。

If there are no application associated with given file type or the application is failed to launch, open method throws java.io.IOException.

如果没有与给定文件类型关联的应用程序,或者应用程序启动失败,则open方法将抛出java.io.IOException

That’s all for a simple program to open a file in java.

这就是一个简单的程序来用Java打开文件。

翻译自:

java 文件 打开文件

转载地址:http://fqozd.baihongyu.com/

你可能感兴趣的文章
在线教育工具—白板系统的迭代1——bug监控排查
查看>>
121. Best Time to Buy and Sell Stock
查看>>
hdu 1005 根据递推公式构造矩阵 ( 矩阵快速幂)
查看>>
安装php扩展
查看>>
百度移动搜索主要有如下几类结果构成
查看>>
Python爬虫面试题170道:2019版【1】
查看>>
JavaBean规范
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
linux 系统下 tar 的压缩与解压缩命令
查看>>
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>