Java Code Examples for java.io.FileNotFoundException#toString()

The following examples show how to use java.io.FileNotFoundException#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorReporterClient.java    From mapbox-events-android with MIT License 6 votes vote down vote up
@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++;
  }
}
 
Example 2
Source File: MemStat.java    From cosmic with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example 3
Source File: CollectionManager.java    From anthelion with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
  }
}
 
Example 4
Source File: DataFormatParserService.java    From datacollector-api with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 5
Source File: DataParserFactory.java    From datacollector with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 6
Source File: CachedOutputStream.java    From cxf with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example 7
Source File: CachedWriter.java    From cxf with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example 8
Source File: CollectionManager.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
  }
}
 
Example 9
Source File: MemStat.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example 10
Source File: SimpleRoadMapPlanner.java    From coordination_oru with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 11
Source File: Missions.java    From coordination_oru with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 12
Source File: DownloadThread.java    From play-apk-expansion with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example 13
Source File: DownloadThread.java    From mobile-manager-tool with MIT License 4 votes vote down vote up
/**
    * 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);
}
   }
 
Example 14
Source File: DownloadThread.java    From travelguide with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example 15
Source File: DownloadThread.java    From Alite with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example 16
Source File: DownloadThread.java    From UnityOBBDownloader with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}