public static void writeFile(){
try {
FileWriter fw=new FileWriter("IOWriterFile.txt");
PrintWriter pw=new PrintWriter(fw);
for (int i=0;i<v.size();i++){
System.out.print(i+":"+v.get(i).toString()+"\n");
pw.write(i+":"+v.get(i).toString()+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我想把Vector中的数据读出来后再写到文件中去,但是为什么读出来后就不能写进文件呢?
应该怎样把字符串数据写进文件呢?
try {
FileWriter fw=new FileWriter("IOWriterFile.txt");
PrintWriter pw=new PrintWriter(fw);
for (int i=0;i<v.size();i++){
System.out.print(i+":"+v.get(i).toString()+"\n");
pw.write(i+":"+v.get(i).toString()+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我想把Vector中的数据读出来后再写到文件中去,但是为什么读出来后就不能写进文件呢?
应该怎样把字符串数据写进文件呢?
BufferedWriter fWriter = new BufferedWriter(fw);fWriter.write("Content ...")这样不就可以了吗
用一个迭带器取出vector中的对象.然后强制转换为String类型,再写入文件
Vector v = new Vector();
Enumeration e = v.elements();
String filestr=(String)e.nextElement //取出指示器正指向的元素对象
e.hasMoreElements 用于返回是否还有元素,这里你用一个循环.
public class Ex_CalWord3
{
public static void main(String[] args)throws Exception
{
FileReader kr=new FileReader("c:\\in the end.txt");//从文件读出
FileWriter fw=new FileWriter("c:\\t.txt"); //文本文件的字符输出流
int c;
while((c=kr.read())!=-1)
fw.write(c);
fw.flush();
fw.close();
}
}
你试下 我在网吧 没调试
import java.util.*;public class FileVector{
static Vector v = new Vector();
static void addContents(){
v.addElement(new String("aa"));
}
public static void writeFile(){
try {
FileWriter fw=new FileWriter("IOWriterFile.txt");
PrintWriter pw=new PrintWriter(fw); for (int i=0;i<v.size();i++){
System.out.print(i+":"+v.get(i).toString()+"\n");
pw.write(i+":"+v.get(i).toString()+"\n"); }
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String [] args){
addContents();
writeFile();
}
}
try {
FileWriter fw=new FileWriter("IOWriterFile.txt");
PrintWriter pw=new PrintWriter(fw);
Enumeration e = v.getElements();
Object temp = null;
while(e.hasMoreElements()){
temp = e.nextElement();
System.out.print(temp.toString()+"\n");
pw.write(temp.toString()+"\n");
pw.flush();}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
试试:)