String s="ada";
char[] a=s.toCharArray(); 将字符串转换成字符数组
public class test{
public static void main(String[]args)
{
File f =new File("D:d/write.txt");
Writer writer=null;
try {
writer=new FileWriter(f);
//写法一
/*String s="adad afaff哈哈哈";
char[] ch=s.toCharArray(); //从字符串到字符数组
writer.write(ch,0,ch.length);
**write.flush();***/
//写法二
/*String s="adad afaff哈哈哈2";
writer.write(s,0,s.length()); //因为字符串就是字符数组,所以可以直接使用
writer.write("addd"); //可实现多次写入
writer.write("fff");
**writer.flush();**
*/
//写法三
/*writer.append("adad").append("aaa\n");
**writer.flush();***/
}catch(FileNotFoundException e)
{
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}finally {
try {
if(null!=writer)
{
**writer.close();**
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
}
原文地址:https://blog.51cto.com/14437184/2423186
时间: 2024-10-09 12:00:07