@Test publicvoidtestWriteWithByteArray()throws IOException { String fileName = "data2.txt"; File file = new File(FILE_DIR + fileName); String lyrics = "I will not make the same mistakes that you did\n" + "I will not let myself cause my heart so much misery\n" + "I will not break the way you did\n" + "You fell so hard\n" + "I learned the hard way, to never let it get that far"; byte[] lyricsArray = lyrics.getBytes(); Files.write(lyricsArray, file); }
@Test publicvoidtestWriteWithCharSequence()throws IOException { String fileName = "data2.txt"; File file = new File(FILE_DIR + fileName); String lyrics = "I will not make the same mistakes that you did\n" + "I will not let myself cause my heart so much misery\n" + "I will not break the way you did\n" + "You fell so hard\n" + "I learned the hard way, to never let it get that far"; CharSequence charSequence = lyrics.subSequence(0, lyrics.length() - 1); Files.write(charSequence, file, Charsets.UTF_8); }
@Test publicvoidtestNewWriter()throws IOException { String fileName = "data2.txt"; File file = new File(FILE_DIR + fileName); String lyrics = "I will not make the same mistakes that you did\n" + "I will not let myself cause my heart so much misery\n" + "I will not break the way you did\n" + "You fell so hard\n" + "I learned the hard way, to never let it get that far"; BufferedWriter bufferedWriter = Files.newWriter(file, Charsets.UTF_8); bufferedWriter.write(lyrics); bufferedWriter.close(); }