C++
#include<fstream>
读文件
需要自己手动建文件
ifstream ifs;
ifs.open("FilePath");
if(!ifs.is_open()){
cout<<"ERROR OPENING FILE";
exit(1);
}
int a,b;
while(ifs>>a){
ifs>>b;
cout<<a<<" "<<b<<endl;
}
ifs.close();
/*文件内容:
1 2
3 4
输出:
1 2
3 4*/
写文件
ofstream ofs;
ofs.open("FilePath");
int a=1,b=2;
ofs<<a<<" "<<b;
ofs.close();
//文件内容:1 2
Java
import java.io.*;
import java.util.*;
读文件
File f_in=new File("FilePath");
if(!f_in.exist())
f_in.createNewFile();
Scanner sc=new Scanner(new FileWriter(f_in));
while(sc.hasNext()){
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println(a+" "+b);
}
sc.close();
/*文件内容:
1 2
3 4
输出:
1 2
3 4*/
写文件
File f_out=new File(FilePath);
FileWriter f_w=new FileWriter(f_out);
f_w.write(1+" "+2);
f_w.close();
//文件内容:1 2
最后一定记得close