欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java讀寫本地文件代碼,java寫入本地文件

Java如何讀寫txt文件的代碼

有關(guān)Java如何讀寫txt文件這個(gè)問題經(jīng)常在面試時(shí)會(huì)被問到,不懂或不熟悉的同志們可是要記好了喲!先來看下具體實(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)是一家集網(wǎng)站建設(shè),泰興企業(yè)網(wǎng)站建設(shè),泰興品牌網(wǎng)站建設(shè),網(wǎng)站定制,泰興網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,泰興網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(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);

// 從輸入流中讀取屬性列表(鍵和元素對(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讀取.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寫入本地文件
當(dāng)前URL:http://chinadenli.net/article22/hddijc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、企業(yè)建站、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站導(dǎo)航、微信公眾號(hào)、Google

廣告

聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)