成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

java文件讀寫代碼 java讀寫文本文件

Java如何讀寫txt文件的代碼

有關(guān)Java如何讀寫txt文件這個(gè)問(wèn)題經(jīng)常在面試時(shí)會(huì)被問(wèn)到,不懂或不熟悉的同志們可是要記好了喲!先來(lái)看下具體實(shí)現(xiàn)吧! package common; import java.io.*; import java.util.ArrayList; public class IOTest { public static void main (String args[]) { ReadDate(); WriteDate(); } /** * 讀取數(shù)據(jù) */ 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(); } } /** * 寫入數(shù)據(jù) */ 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); } } }

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的共青城網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

Java文件讀寫

實(shí)用的模糊(通配符)文件查找程序

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 * 可以根據(jù)正則表達(dá)式查找

31 * @param dir String 文件夾名稱

32 * @param s String 查找文件名,可帶*.?進(jìn)行模糊查詢

33 * @return File[] 找到的文件

34 */

35 public static File[] getFiles(String dir,String s) {

36 //開(kāi)始的文件夾

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點(diǎn)抗 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 * 測(cè)試

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 {

// 根據(jù)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);

// 從輸入流中讀取屬性列表(鍵和元素對(duì))

prop.load(fis);

// 調(diào)用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。

// 強(qiáng)制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調(diào)用 put 的結(jié)果。

OutputStream fos = new FileOutputStream(filePath);

prop.setProperty(parameterName, parameterValue);

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

// 將此 Properties 表中的屬性列表(鍵和元素對(duì))寫入輸出流

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中寫入文件和從文件中讀取數(shù)據(jù)的最佳的代碼!

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);

}

/**

* 傳遞寫的內(nèi)容

* @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();//不存在直接創(chuàng)建

}

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

fw.write(str);

fw.flush();

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 返回讀取的內(nèi)容

* @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){//讀到結(jié)束為止

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

}

br.close();

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

return str;

}

}

剛寫的,夠朋友好好學(xué)習(xí)一下啦,呵呵

多多看API,多多練習(xí)

當(dāng)前標(biāo)題:java文件讀寫代碼 java讀寫文本文件
網(wǎng)頁(yè)網(wǎng)址:http://jinyejixie.com/article6/ddicjog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站內(nèi)鏈定制網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)動(dòng)態(tài)網(wǎng)站、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
皋兰县| 安丘市| 汤阴县| 青铜峡市| 安福县| 蕲春县| 千阳县| 保德县| 集安市| 东乡| 白河县| 红原县| 云安县| 张家口市| 石景山区| 德令哈市| 长沙市| 塔城市| 景德镇市| 梁河县| 承德县| 新巴尔虎右旗| 彩票| 永吉县| 东平县| 冕宁县| 宝兴县| 图木舒克市| 石城县| 广丰县| 五大连池市| 平江县| 许昌县| 长岭县| 宽城| 西安市| 理塘县| 佛冈县| 嫩江县| 江孜县| 蕲春县|