1. Java读写普通文件,以字符串形式读取
private static String fileToJson(String filename){
BufferedReader reader = null;
StringBuffer laststr = new StringBuffer();
try{
FileInputStream fileInputStream = new FileInputStream(filename);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
reader = new BufferedReader(inputStreamReader);
String tempString = null;
while((tempString = reader.readLine()) != null){
laststr.append(tempString);
}
reader.close();
}catch(IOException e){
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return laststr.toString();
}2. 读取Properties文件
public static String ReadRara(String ParaName) throws IOException{
Properties prop = new Properties();// 属性集合对象
String base = ReadPara.class.getResource("/").getPath();
FileInputStream fis = new FileInputStream(base + "/para.properties");//属性文件输入流
prop.load(fis);// 将属性文件流装载到Properties对象中
String para = prop.getProperty(ParaName);
fis.close();// 关闭流
return para;
}
3. Java写文件
public static void main(String[] args) {
FileOutputStream out = null;
try{
File file = new File("IMEI.csv");
out = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(out, "UTF8");
BufferedWriter bw = new BufferedWriter(osw);
for(int i=0;i<count;i++){
imei = (start+i)+"";
is_supgsm = (int) (Math.random()*2);
is_suptd = (int) (Math.random()*2);
is_supwlan = (int) (Math.random()*2);
is_p2p = (int) (Math.random()*2);
bw.write(imei + "," + imei_tac + "," + is_supgsm + "," +is_suptd+","+is_supwlan+","+is_p2p+"\n");
}
bw.close();
osw.close();
}catch(Exception e){
e.printStackTrace();
}finally{
if(out!=null){
out.close();
}
}
}
4.以append方式写入文件
private static void writeLog()
{
try{
FileWriter writer = new FileWriter("facebook.log", true);//第二个参数表示用append方式写入文件
writer.append("zhb\n");
writer.close();
}catch(Exception e){
e.printStackTrace();
}
}

5622

被折叠的 条评论
为什么被折叠?



