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

 我来答
z277416636
2011-06-18
知道答主
回答量:52
采纳率:0%
帮助的人:32.9万
展开全部
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); // 显示文件内容
}
}
barbarianli
推荐于2018-05-10 · 超过27用户采纳过TA的回答
知道答主
回答量:85
采纳率:0%
帮助的人:60.3万
展开全部
方法一:
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();
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
mfvtxrw
2011-06-19 · TA获得超过2866个赞
知道小有建树答主
回答量:1556
采纳率:100%
帮助的人:798万
展开全部
操作数据库才用到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;
}
}
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式