Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

xiaozhi1990
Copy link

张晓智的作业3

Copy link

@dqsdhr dqsdhr left a 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() {
Copy link

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();
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge到master的不是最新的,查看下是否合错了。

@xiaozhi1990
Copy link
Author

merge到master的不是最新的,查看下是否合错了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants