提交 b2ee58eb 编写于 作者: cherry_xixi's avatar cherry_xixi

更新c和c++的基础语法知识和模板.txt, java的基础语法知识和模板.txt, python的基础语法知识和模板.txt, try

上级 54a2256e
读取文件的方法:
1、按字节读取文件内容:(模板)
public class ReadFromFile{
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
/*1*/ File file = new File(fileName);
InputStream in = null;//inputStream为所有输入类的超类,是抽象类,不能实例化
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
/*2*/ in = new FileInputStream(file);// 文件字节输入流,对文件数据以字节的形式进行读取操作
int tempbyte;
/*3*/ while ((tempbyte = in.read()) != -1) {//从输入流中读取一个字节返回int型变量,若到达文件末尾则返回-1
System.out.write(tempbyte);
}
/*4*/ in.close();//关闭文件输入流
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];//字节数组
int byteread = 0;
in = new FileInputStream(fileName);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {//读入多个字节,存在字节数组中,返回字节数,若到末尾则返回-1
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();//关闭输入流
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args) {
String file = "D:\\1study\\hello.txt";
readFileByBytes(file);
}
}
2、按字符读取文件内容:
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
public static void readFileByChars(String fileName) {
/*1*/ File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
/*2*/ reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
/*3*/ while ((tempchar = reader.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
/*4*/ reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3、按行读取文件内容:
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
/*1*/ File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
/*2*/ reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
/*3*/ while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
/*4*/ reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
4、随机读取文件内容 :
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
/*1*/ randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
/*2*/ long fileLength = randomFile.length();
// 读文件的起始位置
/*3*/ int beginIndex = (fileLength > 4) ? 4 : 0;
// 将读文件的开始位置移到beginIndex位置。
/*4*/ randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
/*5*/ while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 显示输入流中还剩的字节数
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
/*文件末尾追加内容*/
/*使用randomaccessfile追加*/
public static void appendMethodA(String fileName, String content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
//将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 使用FileWriter追加
*/
public static void appendMethodB(String fileName, String content) {
try {
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册