资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java文件读写操作代码 java文件读写操作代码是什么

Java文件读写

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

凌云ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

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 * pTitle: FileService /p

8* pDescription: 获取文件 /p

9* pCopyright: Copyright (c) 2007/p

10* pCompany: /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("#", "\\\\.");

41 s = s.replace('*', '#');

42 s = s.replaceAll("#", ".*");

43 s = s.replace('?', '#');

44 s = s.replaceAll("#", ".?");

45 s = "^" + s + "$";

46

47 System.out.println(s);

48 Pattern p = Pattern.compile(s);

49 ArrayList list = filePattern(file, p);

50

51 File[] rtn = new File[list.size()];

52 list.toArray(rtn);

53 return rtn;

54 }

55

56 /** *//**

57 * @param file File 起始文件夹

58 * @param p Pattern 匹配类型

59 * @return ArrayList 其文件夹下的文件夹

60 */

61

62 private static ArrayList filePattern(File file, Pattern p) {

63 if (file == null) {

64 return null;

65 }

66 else if (file.isFile()) {

67 Matcher fMatcher = p.matcher(file.getName());

68 if (fMatcher.matches()) {

69 ArrayList list = new ArrayList();

70 list.add(file);

71 return list;

72 }

73 }

74 else if (file.isDirectory()) {

75 File[] files = file.listFiles();

76 if (files != null files.length 0) {

77 ArrayList list = new ArrayList();

78 for (int i = 0; i files.length; i++) {

79 ArrayList rlist = filePattern(files[i], p);

80 if (rlist != null) {

81 list.addAll(rlist);

82 }

83 }

84 return list;

85 }

86 }

87 return null;

88 }

89

90 /** *//**

91 * 测试

92 * @param args String[]

93 */

94 public static void main(String[] args) {

95 }

96}

跪求Java中写入文件和从文件中读取数据的最佳的代码!

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class IOTest {

public static void main(String[] args) {

String str = "123\r\n456";

writeFile(str);//写

String str1 = readFile();//读

System.out.println(str1);

}

/**

* 传递写的内容

* @param str

*/

static void writeFile(String str) {

try {

File file = new File("d:\\file.txt");

if(file.exists()){//存在

file.delete();//删除再建

file.createNewFile();

}else{

file.createNewFile();//不存在直接创建

}

FileWriter fw = new FileWriter(file);//文件写IO

fw.write(str);

fw.flush();

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 返回读取的内容

* @return

*/

static String readFile() {

String str = "", temp = null;

try {

File file = new File("d:\\file.txt");

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);//文件读IO

while((temp = br.readLine())!=null){//读到结束为止

str += (temp+"\n");

}

br.close();

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

return str;

}

}

刚写的,够朋友好好学习一下啦,呵呵

多多看API,多多练习

Java中对文件进行读写操作的基本类是什么?

Java.io包中包括许多类提供许多有关文件的各个方面操作。\x0d\x0a1 输入输出抽象基类InputStream/OutputStream ,实现文件内容操作的基本功能函数read()、 write()、close()、skip()等;一般都是创建出其派生类对象(完成指定的特殊功能)来实现文件读写。在文件读写的编程过程中主要应该注意异常处理的技术。 \x0d\x0a2 FileInputStream/FileOutputStream: \x0d\x0a用于本地文件读写(二进制格式读写并且是顺序读写,读和写要分别创建出不同的文件流对象); \x0d\x0a本地文件读写编程的基本过程为: \x0d\x0a① 生成文件流对象(对文件读操作时应该为FileInputStream类,而文件写应该为FileOutputStream类); \x0d\x0a② 调用FileInputStream或FileOutputStream类中的功能函数如read()、write(int b)等)读写文件内容; \x0d\x0a③ 关闭文件(close())。 \x0d\x0a3 PipedInputStream/PipedOutputStream: \x0d\x0a用于管道输入输出(将一个程序或一个线程的输出结果直接连接到另一个程序或一个线程的输入端口,实现两者数据直接传送。操作时需要连结); \x0d\x0a4管道的连接: \x0d\x0a方法之一是通过构造函数直接将某一个程序的输出作为另一个程序的输入,在定义对象时指明目标管道对象 \x0d\x0aPipedInputStream pInput=new PipedInputStream(); \x0d\x0aPipedOutputStream pOutput= new PipedOutputStream(pInput); \x0d\x0a方法之二是利用双方类中的任一个成员函数 connect()相连接 \x0d\x0aPipedInputStream pInput=new PipedInputStream(); \x0d\x0aPipedOutputStream pOutput= new PipedOutputStream(); \x0d\x0apinput.connect(pOutput); \x0d\x0a5 管道的输入与输出: \x0d\x0a输出管道对象调用write()成员函数输出数据(即向管道的输入端发送数据);而输入管道对象调用read()成员函数可以读起数据(即从输出管道中获得数据)。这主要是借助系统所提供的缓冲机制来实现的。 \x0d\x0a6随机文件读写: \x0d\x0aRandomAccessFile类(它直接继承于Object类而非InputStream/OutputStream类),从而可以实现读写文件中任何位置中的数据(只需要改变文件的读写位置的指针)。 \x0d\x0a随机文件读写编程的基本过程为: \x0d\x0a① 生成流对象并且指明读写类型; \x0d\x0a② 移动读写位置; \x0d\x0a③ 读写文件内容; \x0d\x0a④ 关闭文件。\x0d\x0a\x0d\x0a七里河团队答疑助人,希望我的回答对你有所帮助

java读取文本文件代码

java读取文本文件的方法有很多 这个例子主要介绍最简单 最常用的BufferedReader类 完整例子如下 package net chinaunix blog hzm text;import java io BufferedReader;import java io FileReader;import java io IOException;public class ReadFile {private String path;public ReadFile(String filePath){path = filePath;}public String[] openFile() throws IOException{FileReader fr = new FileReader(path) BufferedReader textReader = new BufferedReader(fr) String[] textData = new String[readLines()];int i;for(i= ; i readLines() i++){textData[i] = textReader readLine() }textReader close() return textData;}int readLines() throws IOException{FileReader fileToRead = new FileReader(path) BufferedReader bf = new BufferedReader(fileToRead) int numberOfLines = ;@SuppressWarnings( unused )String oneLine;while((oneLine = bf readLine()) != null){numberOfLines++;}bf close() return numberOfLines;}}package net chinaunix blog hzm text;import java io IOException;public class FileData {public static void main(String[] args) throws IOException{String filePath = C:/text txt ;try{ReadFile reader = new ReadFile(filePath) String[] content = reader openFile() int i;for(i= ;icontent length;i++){System out println(content[i]) }}catch(IOException e){System out println( 异常信息 + e getMessage()) }}}java io BufferedReaderThe buffer size may be specified or the default size may be used The default is large enough for most purposes In general each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly such as FileReaders and InputStreamReaders For example BufferedReader in = new BufferedReader(new FileReader( foo in )) will buffer the input from the specified file Without buffering each invocation of read() or readLine() could cause bytes to be read from the file converted into characters and then returned which can be very inefficient Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader java io FileReaderFileReader is meant for reading streams of characters For reading streams of raw bytes consider using a FileInputStream lishixinzhi/Article/program/Java/hx/201311/26249


本文标题:java文件读写操作代码 java文件读写操作代码是什么
链接分享:http://cdkjz.cn/article/ddopsjj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220