姿贺1.字符读写函数 :fgetc和fputc
2.字符串读写函数:fgets和fputs
3.数据块读写函数:freed和fwrite
4.格式化读写函数:fscanf和fprinf
代码已写好,希启谈望对你有帮助,顺便求好评。
package architectureimport java.io.File
import java.io.FileNotFoundException
import java.io.FileReader
import java.io.FileWriter
import java.io.IOException
/**
* 森虚
* @author Wang Zhenhui
* @blog www.soaringroad.com
*
*/
public class AboutIOReaderWriter {
/**
* main
* @param args
*/
public static void main(String[] args) {
String filePath = "d:\\out2.txt"
write(filePath)
read(filePath)
}
/**
* Read
* @param filePath file path
*/
public static void read(String filePath) {
FileReader fr = null
StringBuilder result = new StringBuilder()
try {
fr = new FileReader(new File(filePath))
char[] charArray = new char[1024]
while (fr.read(charArray) != -1) {
result.append(charArray)
}
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
if (fr != null) {
try {
fr.close()
} catch (IOException e) {
e.printStackTrace()
}
}
System.out.println(result.toString())
}
/**
* Write
* @param filePath file path
*/
public static void write(String filePath) {
FileWriter fw = null
try {
fw = new FileWriter(new File(filePath))
for (int i 此旁燃= 1 i <= 50 i++) {
fw.append(String.valueOf(i))
}
fw.flush()
} catch (IOException e) {
e.printStackTrace()
}
if (fw != null) {
try {
fw.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)