FileOutputStream(String name,boolean append) FileOutputStream fos = new FileOutputStream("test.txt",true);
sorry ,看错问题了,我再找找看
//append - if true, then bytes will be written to the end of the file rather than the beginning FileOutputStream public FileOutputStream(String name, boolean append) throws FileNotFoundException Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with name as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown. Parameters: name - the system-dependent file name append - if true, then bytes will be written to the end of the file rather than the beginning
不行啊![DEFAULT] Name = State = 我现在要向 Name 的后面加一个值! 怎么半啊?
用Properties import java.util.*; import java.io.*;public class Test { public static void main(String[] args) { try { Properties p = new Properties(); FileInputStream in = new FileInputStream("test.txt"); p.load(in); in.close(); System.out.println(p.getProperty("Name")); } catch(Exception e) { System.out.println(e.getMessage()); } } }
import java.util.*; import java.io.*;public class Test { public static void main(String[] args) { try { Properties p = new Properties(); FileInputStream in = new FileInputStream("test.txt"); p.load(in);in.close();System.out.println(p.getProperty("Name")); *********************************************// Set Name's valuep.setProperty("Name", "456"); System.out.println(p.getProperty("Name"));*********************************************} catch(Exception e) { System.out.println(e.getMessage()); } } }
import java.util.*; import java.io.*;public class Test { public static void main(String[] args) {try { Properties p = new Properties(); FileInputStream in = new FileInputStream("test.txt"); p.load(in); FileOutputStream out = new FileOutputStream("test.txt"); Enumeration enum = p.propertyNames(); while(enum.hasMoreElements()) { String s = enum.nextElement().toString(); if(s.equals("Name")) { //##################################### // Name is the Key you want to change // 789 is the new value you want to set p.setProperty("Name", "789"); //##################################### } else { //##################################### // Remain orgin value p.setProperty(s, p.getProperty(s)); //##################################### }}in.close(); p.store(out, "Test"); } catch(Exception e) { System.out.println(e.getMessage()); } } }
FileOutputStream fos = new FileOutputStream("test.txt",true);
FileOutputStream public FileOutputStream(String name,
boolean append)
throws FileNotFoundException
Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with name as its argument.
If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.
Parameters:
name - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning
Name =
State = 我现在要向 Name 的后面加一个值! 怎么半啊?
FileOutputStream fos = new FileOutputStream("test.txt",true);
****
append 标志位设为 true 为加入到文件后
FileOutputStream fos = new FileOutputStream("test.txt",true);
****
append 标志位设为 true 为加入到文件后
要不然定位到Name也是比较麻烦的。
import java.util.*;
import java.io.*;public class Test {
public static void main(String[] args) {
try {
Properties p = new Properties();
FileInputStream in = new FileInputStream("test.txt");
p.load(in);
in.close();
System.out.println(p.getProperty("Name"));
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
import java.io.*;public class Test {
public static void main(String[] args) {
try {
Properties p = new Properties();
FileInputStream in = new FileInputStream("test.txt");
p.load(in);in.close();System.out.println(p.getProperty("Name"));
*********************************************// Set Name's valuep.setProperty("Name", "456");
System.out.println(p.getProperty("Name"));*********************************************} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
作,具体的类叫RadomAccessFile。很简单,你看看API把
import java.io.*;public class Test {
public static void main(String[] args) {try {
Properties p = new Properties();
FileInputStream in = new FileInputStream("test.txt");
p.load(in);
FileOutputStream out = new FileOutputStream("test.txt");
Enumeration enum = p.propertyNames();
while(enum.hasMoreElements()) {
String s = enum.nextElement().toString();
if(s.equals("Name")) {
//#####################################
// Name is the Key you want to change
// 789 is the new value you want to set
p.setProperty("Name", "789");
//#####################################
}
else {
//#####################################
// Remain orgin value
p.setProperty(s, p.getProperty(s));
//#####################################
}}in.close();
p.store(out, "Test");
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
PrintWriter out=new PrintWriter(new FileWriter(f.getName(),true),true);记得给分
PrintWriter out=
new PrintWriter(new FileWriter(f.getName),true),true);out.write("AAAA");
out.flush();
out.close();
PrintWriter out=
new PrintWriter(new FileWriter(f.getName(),true),true);
out.println("aaaa");
out.flush();
out.close();