我想使用一个BufferedReader对象从两个或多个文件中读取文本.
这就是我在代码中的做法.
Charset charset = Charset.forName("UTF-8");
Path p1 = Paths.get("sum1.csv");
List<String> list = new ArrayList<String>();
BufferedReader reader = Files.newBufferedReader(p1, charset);
try {
String line;
while((line = reader.readLine()) != null && !line.isEmpty()){
list.add(line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
reader.close();
}
Path p2 = Paths.get("sum2.csv");
reader = Files.newBufferedReader(p2, charset);
try {
String line;
while((line = reader.readLine()) != null && !line.isEmpty()){
list.add(line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
reader.close();
}
Run Code Online (Sandbox Code Playgroud)
代码编译并正确运行.
处理这个问题的标准方法是什么?是否可以使用单个BufferedReader读取两个或更多文件?
我有一个git存储库,位于~/a。
同时,我还有的资料~/b/content/data/,这些资料将由另一个应用程序更新。
为了备份,我想将~/b/content/data/的内容添加到git中~/a,而不移动文件夹。当然,也无需手动复制。
我可以那样做吗?是通过ln吗?
我编写了一个递归函数来查找二叉树的最小值(假设它是无序的)。
代码如下。
//assume node values are positive int.
int minValue (Node n) {
if(n == null) return 0;
leftmin = minValue(n.left);
rightmin = minValue(n.right);
return min(n.data, leftmin, rightmin);
}
int min (int a, int b, int c) {
int min = 0;
if(b != 0 && c != 0) {
if(a<=b) min =a;
else min =b;
if(min<=c) return min;
else return c;
}
if(b==0) {
if(a<=c) return a;
else return c;
}
if(c==0) {
if(a<=b) return a;
else return b; …Run Code Online (Sandbox Code Playgroud) 我理解按字母顺序列出当前目录中文件名的命令是ls.
但是如何从当前目录中的排序文件名中获得前10名呢?