JAVA 读写文件
1,JAVA读写文件主要用到JAVA的IO里边的BufferedReader来读取,BufferedWriter来写文件。
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class MBFileManager { private static final String PATH="E:/test"; //constructor public MBFileManager(){ initFolder(); } public File initFolder(){ File folder=new File(PATH); //folder not exist create a new folder if(!folder.exists()){ folder.mkdir(); } return folder; } public File getFile() { File[] fileList=initFolder().listFiles(); File file2get = null; if(fileList.length>0){ file2get=fileList[fileList.length-1]; } else{ file2get=new File(PATH+"/"+"test"+fileList.length+".txt"); //create a file try { file2get.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return file2get; } public void createAnewFile(){ File[] fileList=initFolder().listFiles(); File file2create=new File(PATH+"/"+"test"+fileList.length+".json"); try { file2create.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void readFile(){ BufferedReader bufReader = null; try { bufReader=new BufferedReader(new InputStreamReader(new FileInputStream(getFile()))); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { String temp=null; while((temp=bufReader.readLine())!=null){ System.out.println(temp); } bufReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void writeFile(String[] content){ createAnewFile(); File file2write = getFile(); String content2write="{" + "\"MemberDB_Coordinate_IMEI\":\"@a@\"" +","+ "\"MemberDB_Coordinate_Longitude\":\"@b@\"" +","+ "\"MemberDB_Coordinate_Latitudes\":\"@c@\"" +","+ "\"MemberDB_Coordinate_CurrentTime\":\"@d@\"" +","+ "\"MemberDB_Coordinate_UploadTime\":\"@e@\"" +","+ "\"Coordinate_VersionCode\":\"@f@\""+ "}"; //replaceFirst这个方法一定要像下面这样写,才能有效果,因为如果你6个下来都是这样的话,不赋值,就保存不了你已经替换的字符串。因为它只管操作不管保存。 content2write=content2write.replaceFirst("@a@", content[0]); content2write=content2write.replaceFirst("@b@", content[1]); content2write=content2write.replaceFirst("@c@", content[2]); content2write=content2write.replaceFirst("@d@", content[3]); content2write=content2write.replaceFirst("@e@", content[4]); content2write=content2write.replaceFirst("@f@", content[5]);
//下面这部分是当文件大小超过5KB的时候就新建一个文件 /*try { if(file2write.length()>5000){ createAnewFile(); file2write=getFile(); } raf=new RandomAccessFile (file2write,"rw"); raf.seek(raf.length()); raf.writeBytes("\n"+content); raf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ try { BufferedWriter bufWriter=new BufferedWriter(new FileWriter(file2write)); bufWriter.write(content2write); bufWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2.字符串的拆分
很多时候拆分字符串的时候我们都用string的subString。但是如果用split();这个方法会更加简单。
public static String[] getValues(String v){
String value=v;
String values[]=value.split(",");
return values;
}
输入一个字符(假如以字符串里面的 ,号为分割)串就会返回字符串数组。