读文件、写文件、删除文件

来源:互联网 发布:淘宝网扇子舞视频 编辑:程序博客网 时间:2024/04/29 13:34

//读文件

String confPath = "D:/dxjk/schedule_report/path_config.properties";

File file = new File(confPath);

BufferedReader reader = null;
try 
{
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
tempString = reader.readLine();
while (tempString != null)
{
if(tempString.indexOf("=")!=-1)
{
String[] conf = tempString.split("=");
if(conf[0].compareTo("PORT")==0)
{
port = conf[1];
}
if(conf[0].compareTo("DB_URL")==0)
{
db_url = conf[1];
}
if(conf[0].compareTo("DB_USERNAME")==0)
{
db_user = conf[1];
}
if(conf[0].compareTo("DB_PASSWORD")==0)
{
db_pass = conf[1];
}
}
line++;
tempString = reader.readLine();

   
}
reader.close();
} catch (IOException e) 
{
e.printStackTrace();
} finally 
{
if (reader != null)
{
try 
{
reader.close();
} catch (IOException e1) 
{}
}

}

————————————————————————————————————————————————

//写文件,有相同文件删除

public boolean DOWriteTxt(String fileName, String content) {
boolean flag=false;
try {

// f.createNewFile();//创建文件
String filePath = ReadPropertiesFile()+fileName;
File f = new File(filePath); 
if (f.exists()) {  
                System.out.print("文件存在"); 
                if(f.delete()){
                System.out.println("删除文件成功");
                }
}
FileOutputStream os = new FileOutputStream(new File(filePath), true);
os.write(content.getBytes("ISO8859_1"));
os.close();
flag = true;
} catch (Exception e) {
  e.printStackTrace();
}
return flag; 
}

原创粉丝点击