标签:字符 Java String System fileName 农大 readByte new 习题
实验题1
阅读下面程序,叙述其功能。
[基本要求] 运行程序,并写出本题程序关键程序(1-7)的功能。
package first;
import java.io.FileReader;
import java.io.IOException;
public class First {
/** Defines the entry point of the program. */
public static void main(String[] args) {
System.out.println("Please enter the file path:");
try {
String fileName = "";
while (true) {
/* 1. */ int readByte = System.in.read();
/* 2. */ if (readByte == -1 || readByte == '\r')
break;
/* 3. */ fileName += (char) readByte;
}
// Reads the file and prints it to the System.out stream.
/* 4. */ char[] buffer = new char[20];
/* 5. */ FileReader reader = new FileReader(fileName);
while (true) {
/* 6. */ int length = reader.read(buffer);
if (length < 0) // Reads a long as there is more data.
break;
/* 7. */ String text = new String(buffer, 0, length);
System.out.print(text);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* 1. 读取键盘上输入的单个字符
* 2. 判断输入的是不是一个字符
* 3. 将字符合成文件名
* 4. 声明一个字符数组
* 5. 创建一个访问文件的字符输入流对象
* 6. 字符读取,如果读到文件末尾,并将字符放入字符数组中,返回-1
* 7. 用文件中的字符创建一个String对象
*/
标签:字符,Java,String,System,fileName,农大,readByte,new,习题 来源: https://blog.csdn.net/qq_53436105/article/details/121732267
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。