간단합니다. Java에서 디렉토리의 내용을 읽고 해당 데이터를 배열이나 일종의 변수에 저장하려면 어떻게해야합니까? 둘째, 어떻게 Java로 외부 파일을 열 수 있습니까?Java에서 디렉토리를 읽는 방법
4
A
답변
11
Java IO API를 사용할 수 있습니다. 구체적으로 java.io.File
, java.io.BufferedReader
, java.io.BufferedWriter
등
열기로 가정하면 읽기 용 파일을 여는 것입니다. Java I/O 기능을 잘 이해하려면 다음 링크를 확인하십시오. http://download.oracle.com/javase/tutorial/essential/io/
아래 코드를 확인하십시오.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileIO
{
public static void main(String[] args)
{
File file = new File("c:/temp/");
// Reading directory contents
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
// Reading conetent
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("c:/temp/test.txt"));
String line = null;
while(true)
{
line = reader.readLine();
if(line == null)
break;
System.out.println(line);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(reader != null)
{
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2
클래스 java.io.File을 사용하면 그렇게 할 수 있습니다. 파일은 파일 및 디렉토리 경로 이름의 추상 표현입니다. File.list() 메소드를 사용하여 파일/디렉토리 목록을 검색 할 수 있습니다.
2
파일과 디렉토리를 조작하는 다양한 방법이있는 Commons IO package도 있습니다.
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
public class CommonsIO
{
public static void main(String[] args)
{
// Read the contents of a file into a String
try {
String contents = FileUtils.readFileToString(new File("/etc/mtab"));
} catch (IOException e) {
e.printStackTrace();
}
// Get a Collection of files in a directory without looking in subdirectories
Collection<File> files = FileUtils.listFiles(new File("/home/ross/tmp"), FileFilterUtils.trueFileFilter(), null);
for (File f : files) {
System.out.println(f.getName());
}
}
}
1
public class StackOverflow {
public static void main(String[] sr) throws IOException{
//Read a folder and files in it
File f = new File("D:/workspace");
if(!f.exists())
System.out.println("No File/Dir");
if(f.isDirectory()){// a directory!
for(File file :f.listFiles()){
System.out.println(file.getName());
}
}
//Read a file an save content to a StringBuiilder
File f1 = new File("D:/workspace/so.txt");
BufferedReader br = new BufferedReader(new FileReader(f1));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null)
sb.append(line+"\n");
System.out.println(sb);
}
}
관련 문제
- 1. Java에서 레지스트리를 읽는 방법
- 2. Java에서 XML 파일을 읽는 방법
- 3. Java에서 파일 바이트를 읽는 방법?
- 4. 웹 시작에서 jar 파일에서 디렉토리를 읽는 방법
- 5. Java에서 Linux의 현재 작업 디렉토리를 얻는 방법
- 6. Java에서 OSX .plist 파일을 읽는 방법?
- 7. Java에서 문자열로 묶은 JSON 문자열을 읽는 방법
- 8. Java에서 xml 파일의 내용을 읽는 방법
- 9. Java에서 TCP 스트림을 읽는 가장 효율적인 방법
- 10. java에서 File 객체에 drawble에서 이미지를 읽는 방법?
- 11. Java에서 double로 읽는 방법은 무엇입니까?
- 12. java에서 csv 파일을 읽는 라이브러리
- 13. PHP opendir을 사용하여 현재 디렉토리 밖에서 디렉토리를 읽는 방법?
- 14. Hadoop에서 디렉토리를 만들고 읽는 방법 - Mapreduce 작업 작업 디렉토리
- 15. 안드로이드 - exsting 로케일 값 디렉토리를 읽는 것
- 16. Lucene 색인을 읽는 방법?
- 17. Java에서 한 위치에 파일을 만들고 필요한 경우 디렉토리를 만드는 방법?
- 18. Java에서 디렉토리를 만들 때 문제가 발생합니다.
- 19. core-java에서 외부 BT 동글로부터 데이터를 읽는 방법?
- 20. Java에서 명령 줄에서 사용자 입력을 읽는 올바른 방법
- 21. 제공된 사용자 이름과 암호를 사용하여 Java에서 파일을 읽는 방법
- 22. Java에서 텍스트 파일을 줄 단위로 가장 빨리 읽는 방법
- 23. Java에서 콘솔에서 단일 문자를 읽는 방법 (사용자가 입력 할 때)?
- 24. Java에서 Buffered IO를 사용하여 파일의 다른 두 위치에서 읽는 방법
- 25. java에서 프로젝트 폴더의 이미지를 읽는 방법은 무엇입니까?
- 26. Java에서 큰 InputStream을 읽는 성능 튜닝
- 27. Java에서 Subversion에 액세스하는 방법
- 28. PHP에서 디렉토리를 만드는 방법
- 29. 선언 된 디렉토리에서 파일을 읽는 방법
- 30. 디렉토리를 가로 지르고 파일 정보를 읽는 Python 프로그램