import java.io.BufferedReader;
在三河等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都網(wǎng)站建設、網(wǎng)站制作 網(wǎng)站設計制作按需求定制制作,公司網(wǎng)站建設,企業(yè)網(wǎng)站建設,品牌網(wǎng)站設計,營銷型網(wǎng)站建設,成都外貿(mào)網(wǎng)站建設公司,三河網(wǎng)站建設費用合理。
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){//讀到結束為止
str += (temp+"\n");
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
剛寫的,夠朋友好好學習一下啦,呵呵
多多看API,多多練習
java文件追加內(nèi)容的三種方法:
方法一:
public static void writeToTxtByRandomAccessFile(File file, String str){
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file,"rw");
long len = randomAccessFile.length();
randomAccessFile.seek(len);
randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法二:
public static void writeToTxtByFileWriter(File file, String content){
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法三:
public static void writeToTxtByOutputStream(File file, String content){
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));
bufferedOutputStream.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e ){
e.printStackTrace();
}finally{
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java追加寫入txt文件代碼及注釋參考如下:
public?void?m()?{
FileWriter?ff=?null;
try?{
//查看C盤是否有a.txt文件來判定是否創(chuàng)建
File?f=new?File("c:\\a.txt");
ff?=?new?FileWriter(f,?true);//將字節(jié)寫入文件末尾處,相當于追加信息。
}?catch?(IOException?e)?{
e.printStackTrace();
}
PrintWriter?p?=?new?PrintWriter(ff);
p.println("這里就可以寫入要追加的內(nèi)容了");//此處為追加內(nèi)容
p.flush();
ff.try?{
f.flush();
p.close();
ff.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
向txt文件寫入內(nèi)容基本思路就是獲得一個file對象,新建一個txt文件,打開I/O操作流,使用寫入方法進行讀寫內(nèi)容,示例如下:
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);
}
}
}
原文出自【比特網(wǎng)】,轉載請保留原文鏈接:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
// 在當前路徑(默認)創(chuàng)建3個非空.doc(當然也可以是.txt,.java…)文件
public class FileTest
{
//遇到異常拋給Java虛擬機處理
public static void main(String[] args)throws IOException
{
//i-1,恒為true,創(chuàng)建無限文件,就成病毒了
for (int i = 0; i 3; i++)
{
//指定要要輸入內(nèi)容的文件名name
String name = "a" + i + ".doc";
//定義一個節(jié)點輸出流FilOutputStream
//通俗點:定義一個水管,水管通向name文件
FileOutputStream out = new FileOutputStream(name);
//使用PrintStream包裝該節(jié)點流,使用PrintStream來輸出字符串
//通俗點:給水管加個水龍頭(PrintStream),這個水龍頭具有放水功能(ps.print())
PrintStream ps = new PrintStream(out);
ps.print("我我我窩窩窩窩窩窩窩窩哦我");
ps.append("你");
}
}
}
一切盡在代碼里!
如下代碼,向D盤temp文件夾下的 test.txt 文件中寫入 Hello World!
import?java.io.File;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.OutputStreamWriter;
public?class?App?{
public?static?void?main(String[]?args)?throws?IOException?{
File?file?=?new?File("d:\\temp\\test.txt");
try?( FileOutputStream?outputStream?=?new?FileOutputStream(file);
OutputStreamWriter?streamWriter?=?new?OutputStreamWriter(outputStream)?)?{
streamWriter.write("Hello?World!");
streamWriter.flush();
}
}
}