资讯

精准传达 • 有效沟通

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

java文件读写代码 java读写文本文件

Java如何读写txt文件的代码

有关Java如何读写txt文件这个问题经常在面试时会被问到,不懂或不熟悉的同志们可是要记好了哟!先来看下具体实现吧! package common; import java.io.*; import java.util.ArrayList; public class IOTest { public static void main (String args[]) { ReadDate(); WriteDate(); } /** * 读取数据 */ public static void ReadDate() { String url = “e:/2.txt”; try { FileReader read = new FileReader(new File(url)); StringBuffer sb = new StringBuffer(); char ch[] = new char[1024]; int d = read.read(ch); while(d!=-1){ String str = new String(ch,0,d); sb.append(str); d = read.read(ch); } System.out.print(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 写入数据 */ public static void WriteDate() { try{ File file = new File(“D:/abc.txt”); if (file.exists()) { file.delete(); } file.createNewFile(); BufferedWriter output = new BufferedWriter(new FileWriter(file)); ArrayList ResolveList = new ArrayList(); for (int i = 0; i 10; i++) { ResolveList.add(Math.random()* 100); } for (int i=0 ;i output.write(String.valueOf(ResolveList.get(i)) + “\n”); } output.close(); } catch (Exception ex) { System.out.println(ex); } } }

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的共青城网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

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 * 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点抗 pile(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读写properties文件的代码

Java代码

package com.LY;

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.Properties;

public class TestMain {

// 根据key读取value

public static String readValue(String filePath, String key) {

Properties props = new Properties();

try {

InputStream in = new BufferedInputStream(new FileInputStream(

filePath));

props.load(in);

String value = props.getProperty(key);

System.out.println(key + value);

return value;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

// 读取properties的全部信息

public static void readProperties(String filePath) {

Properties props = new Properties();

try {

InputStream in = new BufferedInputStream(new FileInputStream(

filePath));

props.load(in);

Enumeration en = props.propertyNames();

while (en.hasMoreElements()) {

String key = (String) en.nextElement();

String Property = props.getProperty(key);

System.out.println(key + Property);

}

} catch (Exception e) {

e.printStackTrace();

}

}

// 写入properties信息

public static void writeProperties(String filePath, String parameterName,

String parameterValue) {

Properties prop = new Properties();

try {

InputStream fis = new FileInputStream(filePath);

// 从输入流中读取属性列表(键和元素对)

prop.load(fis);

// 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。

// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。

OutputStream fos = new FileOutputStream(filePath);

prop.setProperty(parameterName, parameterValue);

// 以适合使用 load 方法加载到 Properties表中的格式,

// 将此 Properties 表中的属性列表(键和元素对)写入输出流

prop.store(fos, "Update '" + parameterName+ "' value");

} catch (IOException e) {

System.err.println("Visit " + filePath + " for updating "

+ parameterName + " value error");

}

}

public static void main(String[] args) {

readValue("info.properties", "url");

writeProperties("info.properties", "age","22");

readProperties("info.properties");

System.out.println("OK");

}

}

跪求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读写文本文件
地址分享:http://cdkjz.cn/article/ddicjog.html
多年建站经验

多一份参考,总有益处

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

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

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220