新聞中心
Java如何讀寫txt文件的代碼
有關(guān)Java如何讀寫txt文件這個問題經(jīng)常在面試時會被問到,不懂或不熟悉的同志們可是要記好了喲!先來看下具體實現(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)是一家集網(wǎng)站建設(shè),泰興企業(yè)網(wǎng)站建設(shè),泰興品牌網(wǎng)站建設(shè),網(wǎng)站定制,泰興網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,泰興網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
跪求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í)
求用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);
// 從輸入流中讀取屬性列表(鍵和元素對)
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 表中的屬性列表(鍵和元素對)寫入輸出流
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讀取.wps后綴名文檔的代碼?
可以通過流的方式加載.wps文檔,下面以讀取文檔中的文字保存到本地為例,你參考看看如何讀取的。
import com.spire.doc.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
public class ReadTextFromWPS {
public static void main(String[] args) throws IOException{
//通過流加載WPS文字文檔
FileInputStream inputStream = new FileInputStream(new File("test.wps"));
Document doc = new Document();
doc.loadFromStream(inputStream, FileFormat.Doc);
//獲取文本保存為String
String text = doc.getText();
//將String寫入Txt
writeStringToTxt(text,"讀取WPS文本.txt");
}
public static void writeStringToTxt(String content, String txtFileName) throws IOException {
FileWriter fWriter= new FileWriter(txtFileName,true);
try {
fWriter.write(content);
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
fWriter.flush();
fWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
讀取結(jié)果:
注意在程序中導(dǎo)入spire.doc.jar。
網(wǎng)站標(biāo)題:java讀寫本地文件代碼,java寫入本地文件
文章路徑:http://www.dlmjj.cn/article/hddijc.html