下面列出了java.io.FileNotFoundException#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@NonNull
CrashEvent nextEvent() {
if (!hasNextEvent()) {
throw new IllegalStateException("No more events can be read");
}
try {
File file = crashReports[fileCursor];
CrashEvent event = parseJsonCrashEvent(FileUtils.readFromFile(file));
if (event.isValid()) {
eventFileHashMap.put(event, file);
}
return event;
} catch (FileNotFoundException fileException) {
throw new IllegalStateException("File cannot be read: " + fileException.toString());
} finally {
fileCursor++;
}
}
public void refresh() {
final File f = new File(MEMINFO_FILE);
try (Scanner scanner = new Scanner(f, "UTF-8")) {
parseFromScanner(scanner);
} catch (final FileNotFoundException ex) {
throw new RuntimeException("File " + MEMINFO_FILE + " not found:" + ex.toString());
}
}
/**
* Save collections into file
*
* @throws Exception
*/
public void save() throws IOException {
try {
final FileOutputStream fos = new FileOutputStream(new File(configfile
.getFile()));
final Document doc = new DocumentImpl();
final Element collections = doc
.createElement(Subcollection.TAG_COLLECTIONS);
final Iterator iterator = collectionMap.values().iterator();
while (iterator.hasNext()) {
final Subcollection subCol = (Subcollection) iterator.next();
final Element collection = doc
.createElement(Subcollection.TAG_COLLECTION);
collections.appendChild(collection);
final Element name = doc.createElement(Subcollection.TAG_NAME);
name.setNodeValue(subCol.getName());
collection.appendChild(name);
final Element whiteList = doc
.createElement(Subcollection.TAG_WHITELIST);
whiteList.setNodeValue(subCol.getWhiteListString());
collection.appendChild(whiteList);
final Element blackList = doc
.createElement(Subcollection.TAG_BLACKLIST);
blackList.setNodeValue(subCol.getBlackListString());
collection.appendChild(blackList);
}
DomUtil.saveDom(fos, collections);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
throw new IOException(e.toString());
}
}
default public DataParser getParser(File file, String fileOffset) throws DataParserException {
try {
return getParser(file.getName(), new FileInputStream(file), fileOffset);
} catch (FileNotFoundException e) {
throw new DataParserException(Errors.DATA_PARSER_00, file.getAbsolutePath(), e.toString(), e);
}
}
public DataParser getParser(File file, String fileOffset)
throws DataParserException {
try {
return getParser(file.getName(), new FileInputStream(file), fileOffset);
} catch (FileNotFoundException e) {
throw new DataParserException(Errors.DATA_PARSER_00, file.getAbsolutePath(), e.toString(), e);
}
}
public InputStream getInputStream() throws IOException {
flush();
if (inmem) {
if (currentStream instanceof LoadingByteArrayOutputStream) {
return ((LoadingByteArrayOutputStream) currentStream).createInputStream();
} else if (currentStream instanceof ByteArrayOutputStream) {
return new ByteArrayInputStream(((ByteArrayOutputStream) currentStream).toByteArray());
} else {
return null;
}
}
try {
InputStream fileInputStream = new TransferableFileInputStream(tempFile);
streamList.add(fileInputStream);
if (cipherTransformation != null) {
fileInputStream = new CipherInputStream(fileInputStream, ciphers.getDecryptor()) {
boolean closed;
public void close() throws IOException {
if (!closed) {
super.close();
closed = true;
}
}
};
}
return fileInputStream;
} catch (FileNotFoundException e) {
throw new IOException("Cached file was deleted, " + e.toString());
}
}
public Reader getReader() throws IOException {
flush();
if (inmem) {
if (currentStream instanceof LoadingCharArrayWriter) {
LoadingCharArrayWriter lcaw = (LoadingCharArrayWriter)currentStream;
return new CharArrayReader(lcaw.rawCharArray(), 0, lcaw.size());
}
return null;
}
try {
InputStream fileInputStream = new FileInputStream(tempFile) {
boolean closed;
public void close() throws IOException {
if (!closed) {
super.close();
maybeDeleteTempFile(this);
}
closed = true;
}
};
streamList.add(fileInputStream);
if (cipherTransformation != null) {
fileInputStream = new CipherInputStream(fileInputStream, ciphers.getDecryptor()) {
boolean closed;
public void close() throws IOException {
if (!closed) {
super.close();
closed = true;
}
}
};
}
return new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
} catch (FileNotFoundException e) {
throw new IOException("Cached file was deleted, " + e.toString());
}
}
/**
* Save collections into file
*
* @throws Exception
*/
public void save() throws IOException {
try {
final FileOutputStream fos = new FileOutputStream(new File(configfile
.getFile()));
final Document doc = new DocumentImpl();
final Element collections = doc
.createElement(Subcollection.TAG_COLLECTIONS);
final Iterator iterator = collectionMap.values().iterator();
while (iterator.hasNext()) {
final Subcollection subCol = (Subcollection) iterator.next();
final Element collection = doc
.createElement(Subcollection.TAG_COLLECTION);
collections.appendChild(collection);
final Element name = doc.createElement(Subcollection.TAG_NAME);
name.setNodeValue(subCol.getName());
collection.appendChild(name);
final Element whiteList = doc
.createElement(Subcollection.TAG_WHITELIST);
whiteList.setNodeValue(subCol.getWhiteListString());
collection.appendChild(whiteList);
final Element blackList = doc
.createElement(Subcollection.TAG_BLACKLIST);
blackList.setNodeValue(subCol.getBlackListString());
collection.appendChild(blackList);
}
DomUtil.saveDom(fos, collections);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
throw new IOException(e.toString());
}
}
public void refresh() {
File f = new File(MEMINFO_FILE);
try (Scanner scanner = new Scanner(f,"UTF-8")) {
parseFromScanner(scanner);
} catch (FileNotFoundException ex) {
throw new RuntimeException("File " + MEMINFO_FILE + " not found:" + ex.toString());
}
}
/**
* Load a roadmap stored in a give directory or file, eventually re-sampling paths at a given step).
* @param fileName The directory or file to load the roadmap from.
* @param distanceBetweenPathPoints The minimum acceptable distance between path poses or -1 if any value is ok
* (re-sampling is not performed in this case).
* If a directory is given, the filename is assumed to he "roadmap.txt".
*/
public void loadRoadMap(String fileName, double distanceBetweenPathPoints) {
try {
String fileOnly;
String pathOnly;
File f = new File(fileName);
if (f.isDirectory()) {
pathOnly = fileName;
if (!pathOnly.endsWith(File.separator)) pathOnly += File.separator;
fileOnly = "roadmap.txt";
}
else {
pathOnly = f.getAbsolutePath().substring(0,f.getAbsolutePath().lastIndexOf(File.separator))+File.separator;
fileOnly = f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf(File.separator)+1);
}
Scanner in = new Scanner(new FileReader(pathOnly+fileOnly));
while (in.hasNextLine()) {
String line = in.nextLine().trim();
if (line.length() != 0 && !line.startsWith("#")) {
String[] oneline = line.split(" |\t");
Pose ps = null;
if (line.contains("->")) {
PoseSteering[] knownPath = Missions.loadPathFromFile(pathOnly+oneline[3]);
if (distanceBetweenPathPoints > 0) knownPath = resamplePath(knownPath,distanceBetweenPathPoints);
paths.put(oneline[0]+oneline[1]+oneline[2], knownPath);
}
else {
ArrayList<String> newLineContents = new ArrayList<String>();
for (int i = 0; i < oneline.length; i++) {
if (!oneline[i].trim().equals("")) newLineContents.add(oneline[i].trim());
}
String locationName = newLineContents.get(0);
ps = new Pose(
Double.parseDouble(newLineContents.get(1)),
Double.parseDouble(newLineContents.get(2)),
Double.parseDouble(newLineContents.get(3)));
locations.put(locationName, ps);
}
}
}
in.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
throw new Error("Unable to load the required scenario: " + e.toString());
}
//Build the graph
buildGraphs();
}
/**
* Load a roadmap stored in a give directory or file.
* @param fileName The directory or file to load the roadmap from.
* If a directory is given, the filename is assumed to he "roadmap.txt"
* (the same convention used in saving, see {@link #saveRoadMap(String)}).
*/
public static void loadRoadMap(String path) {
try {
String fileOnly;
String pathOnly;
File f = new File(path);
if (f.isDirectory()) {
pathOnly = path;
if (!pathOnly.endsWith(File.separator)) pathOnly += File.separator;
fileOnly = "roadmap.txt";
}
else {
pathOnly = f.getAbsolutePath().substring(0,f.getAbsolutePath().lastIndexOf(File.separator))+File.separator;
fileOnly = f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf(File.separator)+1);
}
Scanner in = new Scanner(new FileReader(pathOnly+fileOnly));
while (in.hasNextLine()) {
String line = in.nextLine().trim();
if (line.length() != 0 && !line.startsWith("#")) {
String[] oneline = line.split(" |\t");
Pose ps = null;
if (line.contains("->")) {
PoseSteering[] knownPath = loadPathFromFile(pathOnly+oneline[3]);
paths.put(oneline[0]+oneline[1]+oneline[2], knownPath);
//paths.put(oneline[0]+oneline[1]+oneline[2], oneline[3]);
//metaCSPLogger.info("Loaded path: " + oneline[0]+oneline[1]+oneline[2] + " --> " + oneline[3]);
}
else {
ArrayList<String> newLineContents = new ArrayList<String>();
for (int i = 0; i < oneline.length; i++) {
if (!oneline[i].trim().equals("")) newLineContents.add(oneline[i].trim());
}
String locationName = newLineContents.get(0);
ps = new Pose(
Double.parseDouble(newLineContents.get(1)),
Double.parseDouble(newLineContents.get(2)),
Double.parseDouble(newLineContents.get(3)));
locations.put(locationName, ps);
}
}
}
in.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
throw new Error("Unable to load the required scenario: " + e.toString());
}
Missions.buildGraph();
}
/**
* Prepare the destination file to receive data. If the file already exists,
* we'll set up appropriately for resumption.
*/
private void setupDestinationFile(State state, InnerState innerState)
throws StopRequest {
if (state.mFilename != null) { // only true if we've already run a
// thread for this download
if (!Helpers.isFilenameValid(state.mFilename)) {
// this should never happen
throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,
"found invalid internal destination filename");
}
// We're resuming a download that got interrupted
File f = new File(state.mFilename);
if (f.exists()) {
long fileLength = f.length();
if (fileLength == 0) {
// The download hadn't actually started, we can restart from
// scratch
f.delete();
state.mFilename = null;
} else if (mInfo.mETag == null) {
// This should've been caught upon failure
f.delete();
throw new StopRequest(DownloaderService.STATUS_CANNOT_RESUME,
"Trying to resume a download that can't be resumed");
} else {
// All right, we'll be able to resume this download
try {
state.mStream = new FileOutputStream(state.mFilename, true);
} catch (FileNotFoundException exc) {
throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,
"while opening destination for resuming: " + exc.toString(), exc);
}
innerState.mBytesSoFar = (int) fileLength;
if (mInfo.mTotalBytes != -1) {
innerState.mHeaderContentLength = Long.toString(mInfo.mTotalBytes);
}
innerState.mHeaderETag = mInfo.mETag;
innerState.mContinuingDownload = true;
}
}
}
if (state.mStream != null) {
closeDestination(state);
}
}
/**
* Prepare the destination file to receive data. If the file already exists,
* we'll set up appropriately for resumption.
*/
private void setupDestinationFile(State state, InnerState innerState)
throws StopRequest {
if (!TextUtils.isEmpty(state.mFilename)) { // only true if we've already
// run a thread for this
// download
if (!Helpers.isFilenameValid(state.mFilename)) {
// this should never happen
throw new StopRequest(Downloads.STATUS_FILE_ERROR,
"found invalid internal destination filename");
}
// We're resuming a download that got interrupted
File f = new File(state.mFilename);
if (f.exists()) {
long fileLength = f.length();
if (fileLength == 0) {
// The download hadn't actually started, we can restart from
// scratch
f.delete();
state.mFilename = null;
} else if (mInfo.mETag == null && !mInfo.mNoIntegrity) {
// This should've been caught upon failure
f.delete();
throw new StopRequest(Downloads.STATUS_CANNOT_RESUME,
"Trying to resume a download that can't be resumed");
} else {
// All right, we'll be able to resume this download
try {
state.mStream = new FileOutputStream(state.mFilename,
true);
} catch (FileNotFoundException exc) {
throw new StopRequest(Downloads.STATUS_FILE_ERROR,
"while opening destination for resuming: "
+ exc.toString(), exc);
}
innerState.mBytesSoFar = (int) fileLength;
if (mInfo.mTotalBytes != -1) {
innerState.mHeaderContentLength = Long
.toString(mInfo.mTotalBytes);
}
innerState.mHeaderETag = mInfo.mETag;
innerState.mContinuingDownload = true;
}
}
}
if (state.mStream != null
&& mInfo.mDestination == Downloads.DESTINATION_EXTERNAL) {
closeDestination(state);
}
}
/**
* Prepare the destination file to receive data. If the file already exists,
* we'll set up appropriately for resumption.
*/
private void setupDestinationFile(State state, InnerState innerState)
throws StopRequest {
if (state.mFilename != null) { // only true if we've already run a
// thread for this download
if (!Helpers.isFilenameValid(state.mFilename)) {
// this should never happen
throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,
"found invalid internal destination filename");
}
// We're resuming a download that got interrupted
File f = new File(state.mFilename);
if (f.exists()) {
long fileLength = f.length();
if (fileLength == 0) {
// The download hadn't actually started, we can restart from
// scratch
f.delete();
state.mFilename = null;
} else if (mInfo.mETag == null) {
// This should've been caught upon failure
f.delete();
throw new StopRequest(DownloaderService.STATUS_CANNOT_RESUME,
"Trying to resume a download that can't be resumed");
} else {
// All right, we'll be able to resume this download
try {
state.mStream = new FileOutputStream(state.mFilename, true);
} catch (FileNotFoundException exc) {
throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,
"while opening destination for resuming: " + exc.toString(), exc);
}
innerState.mBytesSoFar = (int) fileLength;
if (mInfo.mTotalBytes != -1) {
innerState.mHeaderContentLength = Long.toString(mInfo.mTotalBytes);
}
innerState.mHeaderETag = mInfo.mETag;
innerState.mContinuingDownload = true;
}
}
}
if (state.mStream != null) {
closeDestination(state);
}
}
/**
* Prepare the destination file to receive data. If the file already exists,
* we'll set up appropriately for resumption.
*/
private void setupDestinationFile(State state, InnerState innerState)
throws StopRequest {
if (state.mFilename != null) { // only true if we've already run a
// thread for this download
if (!Helpers.isFilenameValid(state.mFilename)) {
// this should never happen
throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,
"found invalid internal destination filename");
}
// We're resuming a download that got interrupted
File f = new File(state.mFilename);
if (f.exists()) {
long fileLength = f.length();
if (fileLength == 0) {
// The download hadn't actually started, we can restart from
// scratch
f.delete();
state.mFilename = null;
} else if (mInfo.mETag == null) {
// This should've been caught upon failure
f.delete();
throw new StopRequest(DownloaderService.STATUS_CANNOT_RESUME,
"Trying to resume a download that can't be resumed");
} else {
// All right, we'll be able to resume this download
try {
state.mStream = new FileOutputStream(state.mFilename, true);
} catch (FileNotFoundException exc) {
throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,
"while opening destination for resuming: " + exc.toString(), exc);
}
innerState.mBytesSoFar = (int) fileLength;
if (mInfo.mTotalBytes != -1) {
innerState.mHeaderContentLength = Long.toString(mInfo.mTotalBytes);
}
innerState.mHeaderETag = mInfo.mETag;
innerState.mContinuingDownload = true;
}
}
}
if (state.mStream != null) {
closeDestination(state);
}
}
/**
* Prepare the destination file to receive data. If the file already exists,
* we'll set up appropriately for resumption.
*/
private void setupDestinationFile(State state, InnerState innerState)
throws StopRequest {
if (state.mFilename != null) { // only true if we've already run a
// thread for this download
if (!Helpers.isFilenameValid(state.mFilename)) {
// this should never happen
throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,
"found invalid internal destination filename");
}
// We're resuming a download that got interrupted
File f = new File(state.mFilename);
if (f.exists()) {
long fileLength = f.length();
if (fileLength == 0) {
// The download hadn't actually started, we can restart from
// scratch
f.delete();
state.mFilename = null;
} else if (mInfo.mETag == null) {
// This should've been caught upon failure
f.delete();
throw new StopRequest(DownloaderService.STATUS_CANNOT_RESUME,
"Trying to resume a download that can't be resumed");
} else {
// All right, we'll be able to resume this download
try {
state.mStream = new FileOutputStream(state.mFilename, true);
} catch (FileNotFoundException exc) {
throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,
"while opening destination for resuming: " + exc.toString(), exc);
}
innerState.mBytesSoFar = (int) fileLength;
if (mInfo.mTotalBytes != -1) {
innerState.mHeaderContentLength = Long.toString(mInfo.mTotalBytes);
}
innerState.mHeaderETag = mInfo.mETag;
innerState.mContinuingDownload = true;
}
}
}
if (state.mStream != null) {
closeDestination(state);
}
}