导航:首页 > 编程语言 > java文件读写的类

java文件读写的类

发布时间:2022-11-21 12:40:34

1. java中 File类是什么

在java中File类是可以直接操作文件的类,
它有四个构造函数:
File(String parent,String child)
File(File parent,String child)
File(URI uri)
File(String pathname)
封装了以下主要方法:
canWrite() 返回文件是否可以读写
canRead() 返回文件是否可读
compareTo(File pathname)检查文件路径间的顺序
createNewFile() 当文件不存在时生成文件
delete() 从文件系统内删除该文件
deleteOnExit() 程序顺利结束时删除文件
equals(Object obj) 检查特定对象的路径名是否相等
exists() 判断文件是否存在
getAbsoluteFile() 返回文件完整路径的File实例
getAbsolutePath() 返回文件完整路径
getName() 返回文件名称
getParent() 返回文件父目录路径
getPath() 返回文件路径字符串
getParentFile() 返回文件所在文件夹的路径
hashCode() 返回文件哈希码
isDirectory() 判断该路径指示的是否是目录
isFile() 判断该路径指示的是否是文件
lastModified() 返回该文件最后更改时间标志
length() 返回文件长度
list() 返回文件和目录清单
mkdir() 生成指定的目录
renameTo(File dest) 更改文件名字
setReadOnly() 将文件设置为可读
toString() 返回文件状态的字符串
toURL() 将文件的路径字符串转换成URL
推荐于 2017-11-25
查看全部5个回答
— 你看完啦,以下内容更有趣 —
在java中File是什么意思?有什么作用?
在java中File类是可以直接操作文件的类,
它有四个构造函数:
File(String parent,String child)
File(File parent,String child)
File(URI uri)
File(String pathname)

封装了以下主要方法:
canWrite() 返回文件是否可以读写
canRead() 返回文件是否可读
compareTo(File pathname)检查文件路径间的顺序
createNewFile() 当文件不存在时生成文件
delete() 从文件系统内删除该文件
deleteOnExit() 程序顺利结束时删除文件
equals(Object obj) 检查特定对象的路径名是否相等
exists() 判断文件是否存在
getAbsoluteFile() 返回文件完整路径的File实例
getAbsolutePath() 返回文件完整路径
getName() 返回文件名称
getParent() 返回文件父目录路径
getPath() 返回文件路径字符串
getParentFile() 返回文件所在文件夹的路径
hashCode() 返回文件哈希码
isDirectory() 判断该路径指示的是否是目录
isFile() 判断该路径指示的是否是文件
lastModified() 返回该文件最后更改时间标志
length() 返回文件长度
list() 返回文件和目录清单
mkdir() 生成指定的目录
renameTo(File dest) 更改文件名字
setReadOnly() 将文件设置为可读
toString() 返回文件状态的字符串
toURL() 将文件的路径字符串转换成URL

2. java文件读写

写入的时候,你先读一下之前的文件,如果有内容,就在原有的基础上+"\r\n"+加上要覆盖的内容。
如果没有,直接写入就可以了。

3. Java读写文件的几种方法

java读取配置文件的几种方法如下:
方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
方式二:采用ResourceBundle类读取配置信息,
优点是:可以以完全限定类名的方式加载资源后,直接的读取出来,且可以在非Web应用中读取资源文件。缺点:只能加载类classes下面的资源文件且只能读取.properties文件。

4. java文件读写编程

//FileOperation.java
importjava.io.File;
importjava.io.RandomAccessFile;

/**
*
*@authorHPTODO建立文件操作类
*
*/
publicclassFileOperation{

privateFilefile;

publicFileOperation(){

}

publicFileOperation(StringfileDir){
file=newFile(fileDir);
}

/**
*往文件写进字符串msg
*
*@parammsg
*字符串
*@return写入成功返回TRUE
*/
publicbooleanwriteMsg(Stringmsg){
try{
RandomAccessFileaccessFile=newRandomAccessFile(file+"/src/"
+"user.db","rw");
Filefile1=newFile(file+"/src/"+"user。db");
accessFile.seek(file1.length());
accessFile.write(msg.getBytes());
accessFile.close();
returntrue;
}catch(Exceptione){
e.printStackTrace();
}
returnfalse;
}

}
//Test.java

importjava.io.File;
importjava.util.Scanner;

publicclassTest{
privatestaticScannerinput=newScanner(System.in);
privatestaticFiledir=newFile("");
publicstaticFileOperationop=newFileOperation(dir.getAbsolutePath());

publicstaticvoidwriter(){
Stringmsg="";
System.out.print(" 输进信息:");
while(true){
Stringtemp=input.nextLine();
if(temp.trim().equals("end"))
break;
msg+=temp;
}
if(op.writeMsg(msg))
System.out.println("写入结束,内容:"+msg);

}

publicstaticvoidmain(String[]args){
//System.out.println(dir.getAbsolutePath());
writer();
}

}

5. JAVA文件的读写

public class ReadFromFile {
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
// 一次读多个字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 随机读取文件内容
*/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
// 读文件的起始位置
int beginIndex = (fileLength > 4) ? 4 : 0;
// 将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
/**
* 显示输入流中还剩的字节数
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}

6. java文件如何读取

有几种方法读取吧
File
file
=
new
File("d:\\a.txt");//把D盘目录下的a.txt读取出来,
InputStream
is
=
new
FileInputStream(file);//把文件以字节流读到内存中
第二种是类加载
Demo1.class.getClassLoader().getResourceAsStream("a.txt");//Demo1为当前类名,a.txt在与Demo1.class在同一目录下。
还有其它的就不说了

7. Java文件读写

实用的模糊(通配符)文件查找程序

1 import java.io.File;
2 import java.util.regex.Matcher;
3 import java.util.regex.Pattern;
4 import java.util.ArrayList;
5
6 /** *//**
7 * <p>Title: FileService </p> 8* <p>Description: 获取文件 </p> 9* <p>Copyright: Copyright (c) 2007</p>
10* <p>Company: </p>
11* @author not attributable
12* @version 1.0
13*/
14public class FileService {
15 public FileService() {
16 }
17
18 /** *//**
19 * 在本文件夹下查找
20 * @param s String 文件名
21 * @return File[] 找到的文件
22 */
23 public static File[] getFiles(String s)
24 {
25 return getFiles("./",s);
26 }
27
28 /** *//**
29 * 获取文件
30 * 可以根据正则表达式查找
31 * @param dir String 文件夹名称
32 * @param s String 查找文件名,可带*.?进行模糊查询
33 * @return File[] 找到的文件
34 */
35 public static File[] getFiles(String dir,String s) {
36 //开始的文件夹
37 File file = new File(dir);
38
39 s = s.replace('.', '#');
40 s = s.replaceAll("#", "\\\\.");

8. java 怎么读写文件

IO中的 FileInputStream 和 FileOutputStream 属于字节输入流和输出流,前者是用于读入文件,后者用于写出文件

FileWriter 和 FileReader 属于字符流 ,前者用于读入文件,后者用于写出文件

File file = new File("E:\\abc.txt");
try {
FileInputStream fis = new FileInputStream(file);
//此时文件已经读入

FileOutputStream fos = new FileOutputStream("D:\\a.txt");
//此时是写出文件
fis.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

9. java高性能文件读写的工具类

“若要频繁使用这个类,我们可以重写此构造器以提高性能.”
他的意思似乎是他的这句话就是对他这段代码的最好的补充了吧。(实际上他已经重写了这个构造器。)
大侠,不信,请仔细体会一下,便是。
我以为,他写的这个“提高性能”,其实就是用构造函数的方法,“隐式”地调用了一下read方法,这样实然可以看出是更加方便了,至于效率,人工调用,起码就可以省掉了吧。

10. Java读取文件的几种方式

方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
方式二:采用ResourceBundle类读取配置信息,
优点是:可以以完全限定类名的方式加载资源后,直接的读取出来,且可以在非Web应用中读取资源文件。缺点:只能加载类classes下面的资源文件且只能读取.properties文件。
方式三:采用ClassLoader方式进行读取配置信息
优点是:可以在非Web应用中读取配置资源信息,可以读取任意的资源文件信息
缺点:只能加载类classes下面的资源文件。
方法4 getResouceAsStream
XmlParserHandler.class.getResourceAsStream 与classloader不同

阅读全文

与java文件读写的类相关的资料

热点内容
单片机程序员培训 浏览:988
PHP商城源代码csdn 浏览:634
怎么把电脑里文件夹挪出来 浏览:693
java流程处理 浏览:683
ftp创建本地文件夹 浏览:659
腰椎第一节压缩 浏览:738
xp去掉加密属性 浏览:117
2345怎么压缩文件 浏览:982
迷你夺宝新算法 浏览:407
服务器如何防止木马控制 浏览:715
压缩空气用电磁阀 浏览:742
微信为什么不能设置加密认证 浏览:672
邓伦参加密室逃脱视频 浏览:391
音频压缩编码标准 浏览:300
常提到的app是表示什么 浏览:261
天津程序员传销 浏览:349
下班之后的程序员 浏览:73
检测支持ssl加密算法 浏览:344
衢州发布新闻什么APP 浏览:85
中国移动长沙dns服务器地址 浏览:252