
IO流? 向文件写东西?
fileoutputstream fos=new fileoutputstream(filepath,true)
注意后面那个参数,代表追加模式,即不删除原文件,直接在文件后面添加
import java.io.Fileimport java.io.FileWriter
import java.io.IOException
import java.io.Writer
public class Test1 {
//测试方法
public static void main(String[] args) {
Test1 test1 = new Test1()
try {
test1.appendFile()
test1.appendFile()
} catch (IOException e) {
e.printStackTrace()
}
}
//文件名称,默认存到类路径下
public static final String FILENAME="InputFile1.dat"
public void appendFile() throws IOException {
String outPath = getOutputFileName()
//第二个参数以追加的方式写入文件
Writer writer = new FileWriter(outPath, true)
try{
int begin = (int)'A'
int end = (int)'Z'
for(int i= begini <= endi++){
writer.append((char)i)
if((char)i != 'Z') {
writer.append('\t')
}else{
writer.append('\n')
}
}
}finally{
writer.close()
}
}
private String getOutputFileName() throws IOException {
String path = this.getClass().getResource("").getPath()+"/"+FILENAME
File file = new File(path)
if(!file.exists()) {
System.out.println("文件不存在,创建文件")
file.createNewFile()
}
return path
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)