java怎样把一字符串数组写入.txt文件中,求代码,超级感谢 20
展开全部
import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
public class TestFile {
public static void main(String[] args) throws Exception{
//在d盘上创建一个名为testfile的文本文件
File f = new File("D:"+File.separator+"testfile.txt");
//用FileOutputSteam包装文件,并设置文件可追加
OutputStream out = new FileOutputStream(f,true);
//字符数组
String[] str = {"shanghai","beijing","guangdong","xiamen"};
for(int i =0; i<str.length; i++){
out.write(str[i].getBytes()); //向文件中写入数据
out.write('\r'); // \r\n表示换行
out.write('\n');
}
out.close(); //关闭输出流
System.out.println("写入成功!");
}
}
import java.io.OutputStream;
import java.io.FileOutputStream;
public class TestFile {
public static void main(String[] args) throws Exception{
//在d盘上创建一个名为testfile的文本文件
File f = new File("D:"+File.separator+"testfile.txt");
//用FileOutputSteam包装文件,并设置文件可追加
OutputStream out = new FileOutputStream(f,true);
//字符数组
String[] str = {"shanghai","beijing","guangdong","xiamen"};
for(int i =0; i<str.length; i++){
out.write(str[i].getBytes()); //向文件中写入数据
out.write('\r'); // \r\n表示换行
out.write('\n');
}
out.close(); //关闭输出流
System.out.println("写入成功!");
}
}
展开全部
package demo;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class ArrayIOTest{
public void write(){
String[] str={"a","1","b","c","d"};
ObjectOutputStream oos=null;
try{
oos=new ObjectOutputStream(new FileOutputStream("abc.txt"));
oos.writeObject(str);
}
catch(Exception e){
e.printStackTrace();
}finally{
try{
if(oos!=null)oos.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void read(){
ObjectInputStream ois=null;
try{
ois=new ObjectInputStream(new FileInputStream("abc.txt"));
String[] str=(String[])ois.readObject();
System.out.println(str.length);
}
catch(Exception e){
e.printStackTrace();
}finally{
try{
if(ois!=null)ois.close();
}
catch(Exception e){
e.printStackTrace();
};
}
}
public static void main(String [] arrgs){
ArrayIOTest a=new ArrayIOTest();
a.write();
a.read();
}
}
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class ArrayIOTest{
public void write(){
String[] str={"a","1","b","c","d"};
ObjectOutputStream oos=null;
try{
oos=new ObjectOutputStream(new FileOutputStream("abc.txt"));
oos.writeObject(str);
}
catch(Exception e){
e.printStackTrace();
}finally{
try{
if(oos!=null)oos.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void read(){
ObjectInputStream ois=null;
try{
ois=new ObjectInputStream(new FileInputStream("abc.txt"));
String[] str=(String[])ois.readObject();
System.out.println(str.length);
}
catch(Exception e){
e.printStackTrace();
}finally{
try{
if(ois!=null)ois.close();
}
catch(Exception e){
e.printStackTrace();
};
}
}
public static void main(String [] arrgs){
ArrayIOTest a=new ArrayIOTest();
a.write();
a.read();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String str = "";
FileOutputStream fos = new FileOutputStream("D://Result.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
int [] intArr =new int[]{ 10, -101, 2, 5, 3, 4, 6 };
for (int i = 0; i < intArr.length; i++) {
str += intArr[i] + ",";
}
str = str.substring(0, str.length() - 1);
bw.write(str);
FileOutputStream fos = new FileOutputStream("D://Result.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
int [] intArr =new int[]{ 10, -101, 2, 5, 3, 4, 6 };
for (int i = 0; i < intArr.length; i++) {
str += intArr[i] + ",";
}
str = str.substring(0, str.length() - 1);
bw.write(str);
参考资料: http://blog.163.com/flyemu/blog/static/18411143120111281584468/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
主要采用对象序列化技术:
步骤如下:把字符数组所在的类实现序列化接口Serializable,新建一个字符数组对象,用writeobject(字符数组对象)写入txt文件,求代码再回复我
步骤如下:把字符数组所在的类实现序列化接口Serializable,新建一个字符数组对象,用writeobject(字符数组对象)写入txt文件,求代码再回复我
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询