Java 按行读取文件(解决中文乱码问题)

转载原文

//写文件
File f = new File(fileName);      
        if (!f.exists())   
        {       
            f.createNewFile();      
        }      
        OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"gbk");      
        BufferedWriter writer=new BufferedWriter(write);          
        writer.write(fileContent);      
        writer.close();  
//读文件
 File f = new File(fileName);      
        if(f.isFile()&&f.exists())  
        {       
            InputStreamReader read = new InputStreamReader(new FileInputStream(f),"gbk");       
            BufferedReader reader=new BufferedReader(read);       
            String line;       
            while ((line = reader.readLine()) != null)   
            {        
                fileContent += line;       
            }         
            read.close();  

补充:如不关闭不会写入到目的地,只会写到缓冲区,不写关闭( .close)就必须用 .flush来写到目的地 

 

你可能感兴趣的:(java)