這篇文章主要講解了“commons-io如何使用”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“commons-io如何使用”吧!
引入依賴
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
案例1
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class IoTest {
/**
* 獲取文件的后綴
* 獲取文件的baseName
*/
@Test
public void test3() throws IOException {
String path = "E:\\ideaProjects2\\utils-demo\\src\\test\\resources\\1.txt";
// 獲取baseName
System.out.println(FilenameUtils.getBaseName(path));
// 獲取后綴
System.out.println(FilenameUtils.getExtension(path));
}
/**
* 讀取指定的文本文件的內容為List<String>
*/
@Test
public void test2() throws IOException {
String path = "E:\\ideaProjects2\\utils-demo\\src\\test\\resources\\1.txt";
List<String> list = FileUtils.readLines(new File(path), StandardCharsets.UTF_8);
System.out.println(list.size());
}
/**
* 讀取指定的文本文件的內容為一個字符串
*/
@Test
public void test1() throws IOException {
String path = "E:\\ideaProjects2\\utils-demo\\src\\test\\resources\\1.txt";
String string = FileUtils.readFileToString(new File(path), StandardCharsets.UTF_8);
System.out.println(string);
}
}感謝各位的閱讀,以上就是“commons-io如何使用”的內容了,經過本文的學習后,相信大家對commons-io如何使用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。