问题描述
我现在又一个字符串数组。Strings[][]={"大苏打阿斯达盛大的",[ADSADASSDDASDASDAS]};如上。我现在想通过流的方式读到外部文件里面去。然后用的时候在读进来。我单纯的readUTF不行。乱码有谁可以发一个完整的代码啊!!!不胜感激!
解决方案
解决方案二:
toCharArray()使用这个方法试试
解决方案三:
importjava.io.*;publicclassFileTest{publicstaticvoidread(Stringfilename)throwsException{Filef=newFile(filename);BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream(f)));Stringline="";while((line=br.readLine())!=null){System.out.println(line);}br.close();}publicstaticvoidwrite(Stringfilename,String[]content)throwsException{Filef=newFile(filename);PrintWriterpw=newPrintWriter(f);for(inti=0;i<content.length;i++){System.out.println(content[i]);pw.write(content[i]);if(i<content.length-1)pw.write(System.getProperty("line.separator"));}pw.flush();pw.close();}publicstaticvoidmain(Stringrags[])throwsException{Strings[]={"大苏打阿斯达盛大的","[ADSADASSDDASDASDAS]"};Stringfilename="1.txt";write(filename,s);read(filename);}}
解决方案四:
importjava.io.BufferedReader;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;publicclassArrayWriter{publicstaticvoidmain(Stringargv[])throwsIOException{Strings[][]={{"大苏打阿斯达盛大的11","大苏打阿斯达盛大的12"},{"大苏打阿斯达盛大的21","大苏打阿斯达盛大的22"}};//要写入文件的数组//写文件FileWriterfw=newFileWriter("out.txt");for(String[]line:s){StringBuildersb=newStringBuilder();for(Stringfield:line){sb.append(field);sb.append(",");}sb.append("n");fw.write(sb.toString());}fw.close();//读取文件BufferedReaderbr=newBufferedReader(newFileReader("out.txt"));Stringline;List<String[]>list=newArrayList<String[]>();while((line=br.readLine())!=null){line=line.trim();list.add(line.split(","));}br.close();Stringss[][]=list.toArray(newString[list.size()][]);//从文件读入的数组//只是为了输出和输入一样的格式StringBuildersb=newStringBuilder("{");for(String[]l:ss){sb.append("{");for(Stringfield:l){sb.append(""");sb.append(field);sb.append("",");}sb.deleteCharAt(sb.length()-1);sb.append("},");}sb.deleteCharAt(sb.length()-1);sb.append("}");System.out.println(sb);}}