❶ 對一個txt文件追加一段內容,用java實現
import java.io.File;import java.io.FileWriter;import java.io.Writer;public class Test{ public static void main(String args[]) throws Exception{ File f = new File("d:"+File.separator+"test.txt"); Writer out = null; out = new FileWriter(f,true) //true表示追加 String str = "\r\n你好\r\nHello World!"; out.writer(str); out.close(); }}
❷ java將文字信息追加到指定txt文件
代碼如下:
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStreamWriter;
importjava.util.Random;
publicclassChineseName{
String[]FName=newString[4];
String[]LName=newString[4];
publicChineseName(){
FName[0]="艾|ai";
FName[1]="白|";
FName[2]="蔡|cai";
FName[3]="曹|cao";
LName[0]="ai|皚|艾|哀|埃";
LName[1]="an|安|黯|諳|暗|岸";
LName[2]="ao|奧|傲|敖|驁|遨|翱";
LName[3]="ang|昂|盎";
}
publicstaticvoidmain(String[]args)throwsIOException{
FileOutputStreamoutputStream=newFileOutputStream("d:/test.txt",true);
=newOutputStreamWriter(outputStream);
ChineseNamechineseName=newChineseName();
intj=0;
for(inti=0;i<100;i++){
if(j==10){
j=0;
System.out.println();
streamWriter.append(System.lineSeparator());
}
j++;
Stringname=chineseName.getFName()+chineseName.getLName()+chineseName.getLName()+"";
System.out.print(name);
streamWriter.append(name);
}
streamWriter.append(System.lineSeparator());
streamWriter.flush();
streamWriter.close();
outputStream.close();
}
publicStringgetFName(){
Randomrandom=newRandom();
intZ=random.nextInt(4);
StringFN=FName[Z].split("[|]")[0];
returnFN;
}
publicStringgetLName(){
Randomrandom=newRandom();
intZ=random.nextInt(4);
intz=random.nextInt(LName[Z].split("\|").length-1)+1;
StringLN=LName[Z].split("[|]")[z];
returnLN;
}
}
❸ java輸出txt文件,我想要在原來文件後面繼續添加,用的是什麼函數啊
FileOutputStream out = new FileOutputStream("test.txt",true);
構造函數裡面的第二個參數為true時,代表從文件的結尾開始寫,為false時,代表從頭開始寫
❹ JAVA 如何在已經有的TXT文件下追加內容
很簡單 用RandomAccessFile方法(隨機讀寫文件)
RandomAccessFile Raf=new RandomAccessFile(filename,mode);
filname是文件名
mode為模式即:讀寫/rw
我也有一題希望高手幫忙
http://hi..com/sea%5Fdew/blog/item/e089e4ca67441b44f31fe768.html
❺ java 如何向txt文件中的某一行繼續寫入
Java的RandomAccessFile提供對文件的讀寫功能,與普通的輸入輸出流不一樣的是RamdomAccessFile可以任意的訪問文件的任何地方。這就是「Random」的意義所在。
相關API:
RandomAccessFile(String
name, String
mode)構造器,模式分為r(只讀),rw(讀寫)等
RandomAccessFile.readLine()方法實現對一整行的讀取,並重新定位操作位置
RandomAccessFile.write(byte[] b)用於位元組內容的寫入
示例如下:
RandomAccessFileraf=newRandomAccessFile("f:/1.txt","rw");
inttargetLineNum=10;
intcurrentLineNum=0;
while(raf.readLine()!=null){
if(currentLineNum==targetLineNum){//定位到目標行時結束
break;
}
currentLineNum++;
}
raf.write(" insert".getBytes());
raf.close();
❻ java將文字信息追加到指定txt文件
Stringname=""//你的錄入信息
Filefile=newFile("c:/name.txt");//錄入文件地址
if(!file.exists()){
try{
file.createNewFile();
}catch(IOExceptione){
e.printStackTrace();
}
}
OutputStreamos=null;
try{
os=newFileOutputStream(file,true);//false覆蓋true追加
byte[]b=name.getBytes();
os.write(b);//寫入
os.close();//關閉流
}catch(Exceptione){
e.printStackTrace();
}
❼ java如何對文件追加寫入
java文件追加內容的三種方法:
方法一:
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文件追加的幾種方式
java文件追加內容的三種方法:
方法一:
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:往文件中寫數據,新寫入的數據總是覆蓋原有數據,怎麼能實現追加功能呢
File file=new File("f:/a.txt");
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter(file,true));
bw.write("efg");
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
這里關鍵代碼bw=new BufferedWriter(new FileWriter(file,true));
後面參數的true,就代表即使a.txt裡面有內容,也不會替換。
❿ java如何追加寫入txt文件
java中,對文件進行追加內容操作的三種方法!
importjava.io.BufferedWriter;
importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.OutputStreamWriter;
importjava.io.PrintWriter;
importjava.io.RandomAccessFile;
//如果文件存在,則追加內容;如果文件不存在,則創建文件,追加內容的三種方法
{
@SuppressWarnings("static-access")
publicstaticvoidmain(String[]args){
AppendContentToFilea=newAppendContentToFile();
a.method1();
a.method2("E:\dd.txt","222222222222222");
a.method3("E:\dd.txt","33333333333");
}
方法1:
publicvoidmethod1(){
FileWriterfw=null;
try{
//如果文件存在,則追加內容;如果文件不存在,則創建文件
Filef=newFile("E:\dd.txt");
fw=newFileWriter(f,true);
}catch(IOExceptione){
e.printStackTrace();
}
PrintWriterpw=newPrintWriter(fw);
pw.println("追加內容");
pw.flush();
try{
fw.flush();
pw.close();
fw.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
方法2:
publicstaticvoidmethod2(Stringfile,Stringconent){
BufferedWriterout=null;
try{
out=newBufferedWriter(newOutputStreamWriter(
newFileOutputStream(file,true)));
out.write(conent+" ");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
out.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
方法3:
publicstaticvoidmethod3(StringfileName,Stringcontent){
try{
//打開一個隨機訪問文件流,按讀寫方式
RandomAccessFilerandomFile=newRandomAccessFile(fileName,"rw");
//文件長度,位元組數
longfileLength=randomFile.length();
//將寫文件指針移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content+" ");
randomFile.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}