String inXSL="src/main/resources/abc.xslt";
경로 이름에서 파일을 가져와 처리하려고하는데 주어진 경로 (JAVA)에서 파일이 인식되지 않습니다. 참고로 시스템은 MAC입니다.
파일은 src/main/resources
프로젝트 내부에 있습니다.
이에 대한 입력을 제공하십시오.
애플리케이션이 패키징되고 빌드되면 파일은 다른 클래스와 함께 jar (또는 war)의 클래스 경로 내에 배치됩니다. 리소스 스트림을 사용하여로드해야합니다. 다음은 클래스 경로에서 파일을로드하여 파일의 내용을 인쇄하는 샘플 코드입니다.
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Test {
public static void main(String[] args) throws InterruptedException {
byte[] contents = new byte[1024];
InputStream inStream = Test.class.getClassLoader().getResourceAsStream("abc.xslt");
BufferedInputStream bis = new BufferedInputStream(inStream);
int bytesRead=0;
String strFileContents = null;
try {
while( (bytesRead = bis.read(contents)) != -1){
strFileContents = new String(contents, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(strFileContents);
}
}
중요한 라인은 다음과 같습니다.
InputStream inStream = Test.class.getClassLoader().getResourceAsStream("abc.xslt");
사용량에 따라 입력 스트림은 사용중인 API로 전달됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다