-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
张晓智的作业3 #3
base: master
Are you sure you want to change the base?
张晓智的作业3 #3
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
查找TestX的方法修改。
这样更符合OO的风格。
避免了使用while(true)
@Component | ||
public class DemoService { | ||
|
||
public String demo() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public String demo() {
StringBuilder stringBuilder = new StringBuilder();
String fileName;
ClassPathResource resource = new ClassPathResource(BASE_DOCUMENT);
File file;
try {
file = resource.getFile();
if (file != null && file.exists() && file.isDirectory()) {
String[] list = file.list();
if (list != null && list.length > 0) {
List<String> collect = Arrays.stream(list).sorted((Comparator.reverseOrder())).collect(Collectors.toList());
// TestX 找到最大的X
fileName = collect.get(0);
System.out.println(Arrays.toString(collect.toArray()));
} else {
return "文件不存在";
}
} else {
return "文件路径不正确";
}
} catch (IOException e) {
e.printStackTrace();
return "文件路径不正确";
}
// try-with-resource写法,不用close
try (FileInputStream fin = new FileInputStream(new ClassPathResource(BASE_DOCUMENT + fileName).getFile())) {
FileChannel fc = fin.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024 * 10);
while (fc.read(buffer) != -1) {
buffer.flip();
// 支持中文
stringBuilder.append(StandardCharsets.UTF_8.decode(buffer));
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
return "没有这个文件";
}
return stringBuilder.toString();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merge到master的不是最新的,查看下是否合错了。
merge到master的不是最新的,查看下是否合错了。 |
张晓智的作业3