JAVA 如何打开文件 并获取文件路径 JAVA 如何打开文件 并获取文件路径显示打开对话框 打开文本文件后 获取文件路径并读出文件内容新手 详细可以吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1。java.io.File类 :获取文件路径 2。显示打开对话框 这个市awt,Swing方面的吧? 有现成的控件。FileDialog。3。既然知道了File的路径,通过BufferedReader一行一行的把文件中的内容读出即可。 java读取文件或是文件流的代码,涵盖了读取jar文件中的文件流,网络文件流等,有些读取方式为了防止编码转换带来的问题,采取了动态byte[]的方式读取,源码如下import java.io.BufferedInputStream;import java.io.File;import java.io.BufferedOutputStream;import java.io.IOException;import java.io.FileInputStream;import java.io.FileOutputStream;public class Util { public Util() { } /** * 读取源文件内容 * @param filename String 文件路径 * @throws IOException * @return byte[] 文件内容 */ public static byte[] readFile(String filename) throws IOException { File file =new File(filename); if(filename==null || filename.equals("")) { throw new NullPointerException("无效的文件路径"); } long len = file.length(); byte[] bytes = new byte[(int)len]; BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file)); int r = bufferedInputStream.read( bytes ); if (r != len) throw new IOException("读取文件不正确"); bufferedInputStream.close(); return bytes; } /** * 将数据写入文件 * @param data byte[] * @throws IOException */ public static void writeFile(byte[] data,String filename) throws IOException { File file =new File(filename); file.getParentFile().mkdirs(); BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(file)); bufferedOutputStream.write(data); bufferedOutputStream.close(); } /** * 从jar文件里读取class * @param filename String * @throws IOException * @return byte[] */ public byte[] readFileJar(String filename) throws IOException { BufferedInputStream bufferedInputStream=new BufferedInputStream(getClass().getResource(filename).openStream()); int len=bufferedInputStream.available(); byte[] bytes=new byte[len]; int r=bufferedInputStream.read(bytes); if(len!=r) { bytes=null; throw new IOException("读取文件不正确"); } bufferedInputStream.close(); return bytes; } /** * 读取网络流,为了防止中文的问题,在读取过程中没有进行编码转换,而且采取了动态的byte[]的方式获得所有的byte返回 * @param bufferedInputStream BufferedInputStream * @throws IOException * @return byte[] */ public byte[] readUrlStream(BufferedInputStream bufferedInputStream) throws IOException { byte[] bytes = new byte[100]; byte[] bytecount=null; int n=0; int ilength=0; while((n=bufferedInputStream.read(bytes))>=0) { if(bytecount!=null) ilength=bytecount.length; byte[] tempbyte=new byte[ilength+n]; if(bytecount!=null) { System.arraycopy(bytecount,0,tempbyte,0,ilength); } System.arraycopy(bytes,0,tempbyte,ilength,n); bytecount=tempbyte; if(n<bytes.length) break; } return bytecount; }} 从对话框打开文本文件的话,一般这样:try { JTextArea jta = new JTextArea(); JFileChooser jfc = new JFileChooser("D:\\"); File file = jfc.getSelectedFile(); //获取选中的文件 String filepath=file.getPath(); //获取文件的绝对路径 long length = file.length(); FileReader fr = new FileReader(file); char[] data = new char[length]; fr.read(data, 0, length); //读文件 jta.setText(new String(data, 0, length)); //写入文本域 fr.close(); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } //具体用到的类请参考JDK_API 1.5 filedialog()file.getPath()+file.getName() getPath()返回的String已经包含filename,所以不要加file.getName() file.getPath()等同于file.getParent()+file.getName() class MyReader implements ActionListener{private GsgL container;public MyReader(GsgL container){this.container=container;}public void actionPerformed(ActionEvent e){try{// JTextArea jta=new JTextArea(); System.out.println("aaa"); JFileChooser jfc=new JFileChooser("D:\\"); File file=jfc.getSelectedFile(); String filepath=file.getPath()+file.getName(); long length=file.length(); FileReader fr=new FileReader(file); char []data=new char[1024]; int len=fr.read(data); System.out.println(new String(data,0,len)); fr.close(); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } }}现在我通过按钮事件来 实现打开对话框 编译通过了 但是打不开啊 main函数参数问题 关于proxool连接池 如何从内容中过滤掉图片,只要显示部分文字内容? java 高难度 文件列表排序问题?玩过的进 求救!!!! 急!!!!! 什么是J2EE,J2SE,J2ME呀???有什么区别吗? 各位,关于java读取Oracle中clob字段的问题 关于java中正则的处理 Help!BCB中如何调用一个Java Class且得到其返回值? 麻烦高手帮忙分析下 如何在jar包中封装该jar程序所依赖的jar包
2。显示打开对话框 这个市awt,Swing方面的吧? 有现成的控件。FileDialog。
3。既然知道了File的路径,通过BufferedReader一行一行的把文件中的内容读出即可。
java读取文件或是文件流的代码,涵盖了读取jar文件中的文件流,网络文件流等,有些读取方式为了防止编码转换带来的问题,采取了动态byte[]的方式读取,源码如下import java.io.BufferedInputStream;
import java.io.File;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;public class Util { public Util() {
}
/**
* 读取源文件内容
* @param filename String 文件路径
* @throws IOException
* @return byte[] 文件内容
*/
public static byte[] readFile(String filename) throws IOException { File file =new File(filename);
if(filename==null || filename.equals(""))
{
throw new NullPointerException("无效的文件路径");
}
long len = file.length();
byte[] bytes = new byte[(int)len]; BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
int r = bufferedInputStream.read( bytes );
if (r != len)
throw new IOException("读取文件不正确");
bufferedInputStream.close(); return bytes; } /**
* 将数据写入文件
* @param data byte[]
* @throws IOException
*/
public static void writeFile(byte[] data,String filename) throws IOException {
File file =new File(filename);
file.getParentFile().mkdirs();
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(file));
bufferedOutputStream.write(data);
bufferedOutputStream.close(); } /**
* 从jar文件里读取class
* @param filename String
* @throws IOException
* @return byte[]
*/
public byte[] readFileJar(String filename) throws IOException {
BufferedInputStream bufferedInputStream=new BufferedInputStream(getClass().getResource(filename).openStream());
int len=bufferedInputStream.available();
byte[] bytes=new byte[len];
int r=bufferedInputStream.read(bytes);
if(len!=r)
{
bytes=null;
throw new IOException("读取文件不正确");
}
bufferedInputStream.close();
return bytes;
}
/**
* 读取网络流,为了防止中文的问题,在读取过程中没有进行编码转换,而且采取了动态的byte[]的方式获得所有的byte返回
* @param bufferedInputStream BufferedInputStream
* @throws IOException
* @return byte[]
*/
public byte[] readUrlStream(BufferedInputStream bufferedInputStream) throws IOException {
byte[] bytes = new byte[100];
byte[] bytecount=null;
int n=0;
int ilength=0;
while((n=bufferedInputStream.read(bytes))>=0)
{
if(bytecount!=null)
ilength=bytecount.length;
byte[] tempbyte=new byte[ilength+n];
if(bytecount!=null)
{
System.arraycopy(bytecount,0,tempbyte,0,ilength);
} System.arraycopy(bytes,0,tempbyte,ilength,n);
bytecount=tempbyte; if(n<bytes.length)
break;
}
return bytecount;
}}
JTextArea jta = new JTextArea();
JFileChooser jfc = new JFileChooser("D:\\");
File file = jfc.getSelectedFile(); //获取选中的文件
String filepath=file.getPath(); //获取文件的绝对路径
long length = file.length();
FileReader fr = new FileReader(file);
char[] data = new char[length];
fr.read(data, 0, length); //读文件
jta.setText(new String(data, 0, length)); //写入文本域
fr.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} //具体用到的类请参考JDK_API 1.5
file.getPath()+file.getName()
getPath()返回的String已经包含filename,所以不要加file.getName()
private GsgL container;
public MyReader(GsgL container){
this.container=container;
}
public void actionPerformed(ActionEvent e){
try{
// JTextArea jta=new JTextArea();
System.out.println("aaa");
JFileChooser jfc=new JFileChooser("D:\\");
File file=jfc.getSelectedFile();
String filepath=file.getPath()+file.getName();
long length=file.length();
FileReader fr=new FileReader(file);
char []data=new char[1024];
int len=fr.read(data);
System.out.println(new String(data,0,len));
fr.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
现在我通过按钮事件来 实现打开对话框 编译通过了 但是打不开啊