用java输入输出流自动打开文件后如何在文件末尾追加一行字符串

如题所述

package Chapter07.Characters;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class CharactersDemo_03 {
// 使用RandomAccessFile实现文件的追加,其中:fileName表示文件名;content表示要追加的内容
public static void appendMethod_one(String fileName, String content) {
try {
// 按读写方式创建一个随机访问文件流
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
long fileLength = raf.length();// 获取文件的长度即字节数
// 将写文件指针移到文件尾。
raf.seek(fileLength);
// 按字节的形式将内容写到随机访问文件流中
raf.writeBytes(content);
// 关闭流
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 使用FileWriter实现文件的追加,其中:fileName表示文件名;content表示要追加的内容
public static void appendMethod_two(String fileName, String content) {
try {
// 创建一个FileWriter对象,其中boolean型参数则表示是否以追加形式写文件
FileWriter fw = new FileWriter(fileName, true);
// 追加内容
fw.write(content);
// 关闭文件输出流
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void showFileContent(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println(line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args) {
String fileName = "D:/temp/append.txt";
String content = "Successful operation!";
System.out.println(fileName + "文件的内容如下:");
CharactersDemo_03.showFileContent(fileName); // 显示文件内容
// 按RandomAccessFile的形式追加文件
System.out.println("\n按RandomAccessFile的形式追加文件后的内容如下:");
CharactersDemo_03.appendMethod_one(fileName, content);
CharactersDemo_03.appendMethod_one(fileName, "\n Game is Over! \n");
CharactersDemo_03.showFileContent(fileName); // 显示文件内容
// 按FileWriter的形式追加文件
System.out.println("\n按FileWriter的形式追加文件后的内容如下:");
CharactersDemo_03.appendMethod_two(fileName, content);
CharactersDemo_03.appendMethod_two(fileName, "\n Game is Over! \n");
CharactersDemo_03.showFileContent(fileName); // 显示文件内容
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-05-10
方法一:
RandomAccessFile randomFile = new RandomAccessFile("d:/1.txt", "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
//将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeUTF("我是小白");
randomFile.close();

方法二:
FileWriter writer = new FileWriter("d:/1.txt", true);
writer.write("===>我是小白。");
writer.close();本回答被网友采纳
第2个回答  2011-06-19
操作数据库才用到Driver接口吧,文件读写似乎不需要用它

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.RandomAccessFile;
import java.util.Scanner;

/**
* @author idle~`
* @version 0.1
* @date 2008-12-14
*/
public class IOTest {

// 1
public static void readFile(String fileName){
try{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while(line != null){
System.out.println(line);
line = br.readLine();
}
br.close();
fr.close();
}catch(Exception e){
e.printStackTrace();
}

}

public static void selectStudent(String sid,String fileName){
try{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
boolean flag = false;
while(line != null){
if(line.contains(sid)){
System.out.println(line);
flag = true;
}
line = br.readLine();
}
if(!flag)
System.out.println("No date found");
br.close();
fr.close();
}catch(Exception e){
e.printStackTrace();
}
}

public static void addStudent(String sid,String name,String cid,String email,String phone,String fileName){
try{
RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
String line = rf.readLine();
boolean flag = false;
while(line != null){
if(line.contains(sid)){
System.out.println("ID repeated");
flag = true;
}
line = rf.readLine();
}
if(!flag){
rf.seek(rf.length());
rf.writeChars(sid + " " + name + " " + cid + " " + email + " " + phone + "\n");
}
rf.close();
}catch(Exception e){
e.printStackTrace();
}
}

public static void updateStudent(String sid,String name,String cid,String email,String phone,String fileName){
try{
RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
String line = rf.readLine();
StringBuffer sb = new StringBuffer();
boolean flag = false;
while(line != null){
if(line.contains(sid)){
flag = true;
sb.append(sid + " " + name + " " + cid + " " + email + " " + phone + "\n");
}else{
sb.append(line + "\n");
}
line = rf.readLine();
}
if(!flag){
System.out.println("ID is null");
}else{
rf.writeChars(sb.toString());
System.out.println("----update successfully----");
}
rf.close();
}catch(Exception e){
e.printStackTrace();
}
}

public static void deleteStudent(String sid,String fileName){
try{
RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
String line = rf.readLine();
StringBuffer sb = new StringBuffer();
boolean flag = false;
while(line != null){
if(line.contains(sid)){
flag = true;
}else
sb.append(line + "\n");
line = rf.readLine();
}
if(!flag){
System.out.println("ID is null");
}else{
rf.writeChars(sb.toString());
System.out.println("----delete successfully----");
}
rf.close();
}catch(Exception e){
e.printStackTrace();
}
}

public static void delete(String fileName){
try{
RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
rf.setLength(0);
System.out.println("----empty----");
rf.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
String fileName = "D:\\list.dat";
StringBuffer sb = new StringBuffer();
sb.append("------------------------------\n");
sb.append("1.列出所有数据\n");
sb.append("2.查询一行数据通过学生的学号\n");
sb.append("3.添加数据\n");
sb.append("4.修改一行数据通过学生的学号\n");
sb.append("5.删除一行数据通过学生的学号\n");
sb.append("6.删除所有的数据\n");
sb.append("7.退出\n");
sb.append("------------------------------");
Scanner scanner = new Scanner(System.in);
boolean t = true;
while(t){
System.out.println(sb.toString());
int flag = scanner.nextInt();
switch(flag){
case 1:
IOTest.readFile(fileName);
break;
case 2:
System.out.print("Student id:");
String sid = scanner.next();
IOTest.selectStudent(sid, fileName);
break;
case 3:
System.out.print("Student id:");
String sid3 = scanner.next();
System.out.print("\nStudent name:");
String name3 = scanner.next();
System.out.print("\nclass id:");
String cid3 = scanner.next();
System.out.print("\nemail:");
String email3 = scanner.next();
System.out.print("\nphone:");
String phone3 = scanner.next();
IOTest.addStudent(sid3, name3, cid3, email3, phone3, fileName);
break;
case 4:
System.out.print("Student id:");
String sid4 = scanner.next();
System.out.print("\nStudent name:");
String name4 = scanner.next();
System.out.print("\nclass id:");
String cid4 = scanner.next();
System.out.print("\nemail:");
String email4 = scanner.next();
System.out.print("\nphone:");
String phone4 = scanner.next();
IOTest.updateStudent(sid4, name4, cid4, email4, phone4, fileName);
break;
case 5:
System.out.print("Student id:");
String sid5 = scanner.next();
IOTest.deleteStudent(sid5,fileName);
break;
case 6:
System.out.println("are you sure(Y/N)?");
String s = scanner.next();
if(s.equalsIgnoreCase("Y")){
IOTest.delete(fileName);
}
break;
case 7:
t = false;
break;
}
}
}

}
相似回答