如何在文件的下一行中写入内容?并在文件中查找字符串并在同一行

时间:2016-11-26 16:51:10

标签: java replace newline filewriter bufferedwriter

我正在使用SpringBoot应用程序并引入了一个" Like"显示狗图片的按钮和每个图片的按钮会点击下面@RequestMapping(value="likedDog")我正在用分隔符连接必要的信息。

控制器内部的方法如下所示:

@RequestMapping(value = "likedDog")
    public String likedDogList(@RequestParam("breedType") String breedType, @RequestParam("id") String id, Model model){

        Long likes=0L;
        String delimiter="_!_";



        Dogs dogObject= dogsService.returnDogById(Long.valueOf(id));
        if(dogObject.getLikes()==null){
        likes=0L;
        }
        else{
            likes=dogObject.getLikes();
        }


        likes=likes+1;//1,2
        dogObject.setLikes(likes);//set 1,2
        dogObject=dogsService.saveDog(dogObject);

        model.addAttribute("dogObj",dogObject);
        model.addAttribute("dogBreedType",breedType);

        String content=id+delimiter+breedType+delimiter+dogObject.getImgURL()+delimiter+dogObject.getLikes();
        String contentSub=id+delimiter+breedType+delimiter+dogObject.getImgURL();



        try{
        File file = new File("src/main/resources/data/DogLikes.txt");


        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write(content);
        bw.newLine();
        bw.flush();     //  ORIGINAL
        bw.close();
        }
        catch(Exception e){
            System.out.println("Exception occured in likedDogList()");
            e.printStackTrace();
        }


        return "liked";
    }

我想写连接字符串"内容"在相应文件中的换行符上如果喜欢属于不同的狗,代码如下所示,即使我介绍bw.newLine我不能用换行符写它而不是它只是替换文件第一行中的旧现有内容

File DogLikes.txt中的内容如下所示:

17_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_1 //but on every click the contents are being replaced in this first line only

预期输出,每个Like上写入DogLikes.txt的内容如下所示:

17_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_1
18_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_1 //should be written like this in newline on like of dogId 18
19_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_1 //should be written like this in newline on like of dogId 19

第二个问题: 如果我点击"喜欢"在DogId 18上应该按照以下步骤更换第二行,

17_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_1
18_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_2 //replacement of String in this exact location is expected
19_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_1

您的帮助非常明显,提前致谢。

嘿@daniel备份方法用于逐行将内容写入文件,而我正在使用不同的方法进行恢复。然而,我提出了一个新想法,将喜欢和不喜欢的内容整合到同一行中,如下所示:

点击后的预期输出"喜欢"和"不喜欢"

   1_!_Labrador_!_http://i.imgur.com/eE29vX4.png_!_like_!_11_!_dislike_!_5
    2_!_Labrador_!_http://i.imgur.com/xX2AeDR.png_!_like_!_3_!_dislike_!_5
    3_!_Labrador_!_http://i.imgur.com/hBFRUuW.png_!_like_!_1_!_dislike_!_5
    16_!_Pug_!_http://i.imgur.com/E5vBM5Z.png_!_like_!_119_!_dislike_!_5

我在backup()方法中需要你的帮助,因为我尝试了不同的组合来更新同一行中的喜欢和不喜欢,因为你的方法只是动态更新喜欢。此外,我还希望根据最低的喜欢对狗图像进行排序。 感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

正如您在问题中所述,您的要求是使用内存存储和备份机制来处理文件系统中的特定文件。我尝试了一个使用spring mvc和数据休息的简短原型。你可以在这里找到:https://github.com/dbubenheim/stackoverflow-40820684

最有趣的部分是数据的备份和恢复过程,我有以下两个服务类:

@Service
public class DogBackupServiceImpl implements DogBackupService {

    @Value("${dog.backup.file}")
    private String dogLikesFile;

    private static final String DELIMITER = "_!_";

    @Override
    public void backup(final Dog dog) throws IOException {
        final Path dogLikesPath = Paths.get(this.dogLikesFile);
        if (!Files.exists(dogLikesPath)) {
            Files.createDirectories(dogLikesPath.getParent());
            Files.createFile(dogLikesPath);
        }
        final File dogLikesFile = Paths.get(this.dogLikesFile).toFile();
        final StringBuilder stringBuilder = new StringBuilder();
        String currentLine;
        String oldLine = null;
        try (final Scanner scanner = new Scanner(dogLikesFile)) {
            while (scanner.hasNextLine()) {
                currentLine = scanner.nextLine().trim();
                stringBuilder.append(currentLine);
                if (currentLine != null && !currentLine.isEmpty()) {
                    stringBuilder.append("\n");
                }
                if (currentLine != null && currentLine.startsWith(Long.toString(dog.getId()))) {
                    oldLine = currentLine;
                }
            }
        }
        String content = stringBuilder.toString();
        final boolean firstLike = (oldLine == null || oldLine.isEmpty());
        final String updateLine = dog.getId() + DELIMITER + dog.getBreedType() + DELIMITER + dog.getImgUrl() + DELIMITER + dog.getLikes();
        if (!firstLike) {
            content = content.replace(oldLine, updateLine);
        }
        try (final BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(dogLikesFile))) {
            bufferedWriter.write(content);
            if (firstLike) {
                bufferedWriter.write(updateLine);
            }
            bufferedWriter.close();
        }
    }
}

@Service
public class DogRestoreServiceImpl extends ContextLoaderListener implements DogRestoreService {

    @Value("${dog.backup.file}")
    private String dogLikesFile;

    private static final int FIELD_ID = 0;
    private static final int FIELD_BREED_TYPE = 1;
    private static final int FIELD_IMG_URL = 2;
    private static final int FIELD_LIKES = 3;
    private static final String DELIMITER = "_!_";

    private final DogRepository dogRepository;

    public DogRestoreServiceImpl(final DogRepository dogRepository) {
        this.dogRepository = requireNonNull(dogRepository, "dogRepository must not be null!");
    }

    @Override
    public void restore() throws IOException {
        final Path path = Paths.get(this.dogLikesFile);
        if (!Files.exists(path)) {
            System.out.println("nothing to restore");
            return;
        }
        final File dogLikesFile = path.toFile();
        final Set<Dog> dogs = new HashSet<>();
        String line;
        try (final BufferedReader reader = new BufferedReader(new FileReader(dogLikesFile))) {
            while ((line = reader.readLine()) != null) {
                final String[] fields = line.split(DELIMITER);
                final Dog dog = new Dog.DogBuilder(Long.parseLong(fields[FIELD_ID]))
                    .breedType(fields[FIELD_BREED_TYPE])
                    .imgUrl(fields[FIELD_IMG_URL])
                    .likes(Long.parseLong(fields[FIELD_LIKES]))
                    .build();
                dogs.add(dog);
            }
            this.dogRepository.save(dogs);
        }
    }

    @Override
    public void contextInitialized(final ServletContextEvent event) {
        try {
            System.out.println("context initialized. restoring data...");
            this.restore();
            System.out.println("data restoring completed!");
        } catch (final IOException ioException) {
            ioException.printStackTrace();
        }
    }
}    

这个想法是:每当spring数据存储库保存一个enitity以使备份文件保持最新时,备份过程就会保存。 当spring上下文成功初始化(应用程序启动时)时,基本上会触发恢复过程。因此,您应该能够将所有数据都存储在内存和备份文件中。

您可以通过cURL与服务进行交互,并执行以下基本操作:

# retrieve all dogs
curl -X GET "http://localhost:8080/dogs"

# post dog
curl -X POST -H "Content-Type: application/json" -d '{
    "breedType": "Husky",
    "imgUrl": "http://i.imgur.com/abc123.png",
    "likes": 0
}' "http://localhost:8080/dogs"

# like dog
curl -X POST -H "Content-Type: application/json" -d '' http://localhost:8080/dogs/likeDog/<dogs-id>"

希望有所帮助!如果有任何进一步的问题,请告诉我。