❶ 对一个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();
}
}
}