怎么用JAVA打开外来文件啊?不是txt形式的……

怎么用JAVA打开外来文件啊?不是txt形式的……,第1张

一、用eclipse或者myeclipse工具:在左边任意选中一个项目,如果没有新建一个(方法:file文件—>new新建—>project工程或者其他工程)[Import导入]—>[Existing已经存在的项目文件]—>[Browse浏览目录]—>导入你想要打开的外来文件即可完成!

二、举例+图文并茂哈

图1:

图2:

图3:

图4:

三、假设用记事本或者editplus等弱工具打开

①可以先配置好系统变量 path、classpath、java_home

②(1)打开winxp系统 开始-运行-cmd 输入 dir c(您的java jdk具体绝对路径例如c:program Files\jdk16\)

||

||

||

V

(2)输入javac

③输入要打开的“您提到的非txt的文件”文件所在绝对路径 回车 将d出黑色的系统控制台 嘿嘿完成!

首先,你得下载一个jdk,安装完之后,在我的电脑-》属性-》高级-》环境变量-》->在后面加上一个分号和你jdk安装目录/bin,如d:\soft\java\bin再新建一个变量classpath 在值上加上;
记住后面有一个再在命令行提示符上打上javac和java命令,看能出来结果吗,如果出来结果就证明配置成功。再打命令行javac “程序的主程序”java
编译一下,java “程序的主程序”就可运行
实在不行,你下载一个eclipse,把程序放到上面,运行主程序就是有main方法,就可以了

public static void main(String args[]) {
try {
Process process = RuntimegetRuntime()exec(
"cmdexe /c notepad c:/123txt");
} catch (Exception e) {
eprintStackTrace();
}
}

jar文件是Java项目生成的一个小的文件项目,也可以描述为一个Java压缩包,里面封装
了,许多Java类以及方法,变量。本篇经验讲解下如何查看Java生成的jar文件。
首先我们先找到一个Java生成的jar文件,把它存放在指定位置已方便查看
然后右键点击jar文件,选择打开方式,然后设定为压缩文件打开的方式,这时候我们就会看到桌面上的jar文件变成了一个压缩包的图标。
然后我们双击桌面的jar文件,就可以以压缩包的形式查看jar文件里面的具体内容了。
我们要确保自己电脑上已经安装了Java运行环境,这样才能利用Java程序打开jar文件。
在确保安装完成Java程序之后,点击jar文件选择右键,选择默认打开方式,选择Java(TM)platform
SE
进行打开。
设置完成之后,我们右键jar文件进行解压然后也可以查看jar文件的内容了。

如果我们要读取HelloWorldclass的文件:
首先构造数据输入流对象:
FileInputStream fis=new FileInputStream("HelloWorldclass");
DataInputStream dis=new DataInputStream(fis);
这里引入javaioFileInputStream类和javaioDataInputStream类
接下来就可以利用数据输入流类的方法来读取二进制文件的数据了:
disreadInt()--读出来是整形
disreadByte()--读出来是字节类型
最后关闭输入流:
disclose();
以上基本完成要求,希望能有所帮助!

java有个类Desktop(javaawtDesktop),可以满足你的需求,比如下面的代码:
try {
DesktopgetDesktop()open(new File("D:\\1txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
路径为本地磁盘的全路径,就可以打开D盘下的1txt,希望能帮到你。

给你一段文件 *** 作的例子
package comfilesample;
import javaio;
public class FileOperate {
public FileOperate() {
}
/
新建目录

@param folderPath
String 如 c:/fqf
@return boolean
/
public void newFolder(String folderPath) {
try {
String filePath = folderPath;
filePath = filePathtoString();
javaioFile myFilePath = new javaioFile(filePath);
if (!myFilePathexists()) {
myFilePathmkdir();
}
} catch (Exception e) {
Systemoutprintln("新建目录 *** 作出错");
eprintStackTrace();
}
}
/
新建文件

@param filePathAndName
String 文件路径及名称 如c:/fqftxt
@param fileContent
String 文件内容
@return boolean
/
public void newFile(String filePathAndName, String fileContent) {
try {
String filePath = filePathAndName;
filePath = filePathtoString();
File myFilePath = new File(filePath);
if (!myFilePathexists()) {
myFilePathcreateNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = fileContent;
myFileprintln(strContent);
resultFileclose();
} catch (Exception e) {
Systemoutprintln("新建目录 *** 作出错");
eprintStackTrace();
}
}
/
删除文件

@param filePathAndName
String 文件路径及名称 如c:/fqftxt
@param fileContent
String
@return boolean
/
public void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePathtoString();
javaioFile myDelFile = new javaioFile(filePath);
myDelFiledelete();
} catch (Exception e) {
Systemoutprintln("删除文件 *** 作出错");
eprintStackTrace();
}
}
/
删除文件夹

@param filePathAndName
String 文件夹路径及名称 如c:/fqf
@param fileContent
String
@return boolean
/
public void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePathtoString();
javaioFile myFilePath = new javaioFile(filePath);
myFilePathdelete(); // 删除空文件夹
} catch (Exception e) {
Systemoutprintln("删除文件夹 *** 作出错");
eprintStackTrace();
}
}
/
删除文件夹里面的所有文件

@param path
String 文件夹路径 如 c:/fqf
/
public void delAllFile(String path) {
File file = new File(path);
if (!fileexists()) {
return;
}
if (!fileisDirectory()) {
return;
}
String[] tempList = filelist();
File temp = null;
for (int i = 0; i < tempListlength; i++) {
if (pathendsWith(Fileseparator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + Fileseparator + tempList[i]);
}
if (tempisFile()) {
tempdelete();
}
if (tempisDirectory()) {
delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);// 再删除空文件夹
}
}
}
/
复制单个文件

@param oldPath
String 原文件路径 如:c:/fqftxt
@param newPath
String 复制后路径 如:f:/fqftxt
@return boolean
/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfileexists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ((byteread = inStreamread(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
Systemoutprintln(bytesum);
fswrite(buffer, 0, byteread);
}
inStreamclose();
}
} catch (Exception e) {
Systemoutprintln("复制单个文件 *** 作出错");
eprintStackTrace();
}
}
/
复制整个文件夹内容

@param oldPath
String 原文件路径 如:c:/fqf
@param newPath
String 复制后路径 如:f:/fqf/ff
@return boolean
/
public void copyFolder(String oldPath, String newPath) {
try {
(new File(newPath))mkdirs(); // 如果文件夹不存在 则建立新文件夹
File a = new File(oldPath);
String[] file = alist();
File temp = null;
for (int i = 0; i < filelength; i++) {
if (oldPathendsWith(Fileseparator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + Fileseparator + file[i]);
}
if (tempisFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath
+ "/" + (tempgetName())toString());
byte[] b = new byte[1024 5];
int len;
while ((len = inputread(b)) != -1) {
outputwrite(b, 0, len);
}
outputflush();
outputclose();
inputclose();
}
if (tempisDirectory()) {// 如果是子文件夹
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
} catch (Exception e) {
Systemoutprintln("复制整个文件夹内容 *** 作出错");
eprintStackTrace();
}
}
/
移动文件到指定目录

@param oldPath
String 如:c:/fqftxt
@param newPath
String 如:d:/fqftxt
/
public void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/
移动文件到指定目录

@param oldPath
String 如:c:/fqftxt
@param newPath
String 如:d:/fqftxt
/
public void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
public static void main(String[] args){
FileOperate filedemo=new FileOperate();
filedemodelAllFile("d:/test");
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12612770.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-26
下一篇2025-08-26

发表评论

登录后才能评论

评论列表(0条)

    保存