List of usage examples for java.nio.file FileSystems getDefault
public static FileSystem getDefault()
From source file:br.ufc.deti.ecgweb.domain.exam.EcgService.java
@Transactional(timeout = 0) public void importEcg(Long patientId, File file, String ecgFileKey) throws IOException { System.out.println("Map Size=" + uploadFileMap.size()); EcgFileType type = EcgFileType.HL7;/*ww w. j av a 2 s . c o m*/ lock.lock(); try { if (getUploadFile(patientId) != null) { //Has upload file occurring throw new ServiceUploadDuplicatedActionException(); } else { addUploadFile(patientId, file); } } finally { lock.unlock(); } System.out.println("Map Size=" + uploadFileMap.size()); AbstractEcgFormat hl7 = new HL7FormatImpl(); hl7.loadInformationFromFile(file); String newfileName = ecgFileKey; Patient patient = patientRepository.findOne(patientId); EcgFile ecgFile = new EcgFile(); ecgFile.setDate(LocalDateTime.now()); ecgFile.setFileName(newfileName); ecgFile.setType(type); ecgFileRepository.save(ecgFile); Ecg ecg = new Ecg(); ecg.setBaseLine(hl7.getBaseLine()); ecg.setGain(hl7.getGain()); ecg.setSampleRate(hl7.getSampleRate()); ecg.setFinished(Boolean.FALSE); ecg.setFile(ecgFile); ecgRepository.save(ecg); for (int i = 0; i < hl7.getNumberOfChannels(); i++) { EcgChannel channel = new EcgChannel(); channel.setLeadType(hl7.getChannelType(i)); long count = 0; List<EcgSignal> signals = hl7.getChannelSignals(i); for (EcgSignal signal : signals) { count++; /*if (count == 30000) { break; }*/ EcgSignal signal_ = new EcgSignal(); signal_.setSampleIdx(signal.getSampleIdx()); signal_.setyIntensity(signal.getyIntensity()); ecgSignalRepository.save(signal_); channel.addSignal(signal_); if (count % 10000 == 0) { System.out.println(patientId + " : signal:" + count); } } channel.setEcg(ecg); ecgChannelRepository.save(channel); ecg.addChannel(channel); ecgRepository.save(ecg); } patient.addEcgExam(ecg); patientRepository.save(patient); ecg.setFinished(Boolean.TRUE); ecgRepository.save(ecg); Path pathIn = FileSystems.getDefault().getPath(file.getAbsolutePath()); Path pathOut = FileSystems.getDefault().getPath("/home/ecgs/" + newfileName); Files.copy(pathIn, pathOut); Files.deleteIfExists(pathIn); lock.lock(); try { removeUploadFile(patientId); } finally { lock.unlock(); } System.out.println("Map Size=" + uploadFileMap.size()); }
From source file:it.units.malelab.ege.util.DUMapper.java
private static double[][][] getNeatData2(String baseDir, String fileNamePattern, int generations) throws IOException { List<List<Map<Integer, Pair<Double, Double>>>> data = new ArrayList<>(); int maxInnovationNumber = 0; for (int g = 0; g < generations; g++) { List<Map<Integer, Pair<Double, Double>>> currentPopulation = new ArrayList<>(); BufferedReader reader = Files.newBufferedReader( FileSystems.getDefault().getPath(baseDir, String.format(fileNamePattern, g + 1))); String line;/*from w w w . j a va 2s . c om*/ boolean isInPopulation = false; Map<Integer, Pair<Double, Double>> currentIndividual = null; Set<Integer> currentIndividualConnectedNodes = new HashSet<>(); while ((line = reader.readLine()) != null) { if (line.equals("[NEAT-POPULATION:SPECIES]")) { isInPopulation = true; continue; } if (!isInPopulation) { continue; } if (line.startsWith("\"g\"")) { if (currentIndividual != null) { //compute data for nodes for (int usedNode : currentIndividualConnectedNodes) { currentIndividual.put(usedNode, new Pair<>(1d, 1d)); } //save current individual currentPopulation.add(currentIndividual); } currentIndividual = new HashMap<>(); currentIndividualConnectedNodes.clear(); } if (line.startsWith("\"n\"") || line.startsWith("\"l\"")) { String[] pieces = line.split(","); int innovationNumber = Integer.parseInt(pieces[pieces.length - 1]); maxInnovationNumber = Math.max(innovationNumber, maxInnovationNumber); double used = 0; double value = 0; if (line.startsWith("\"l\"")) { value = Double.parseDouble(pieces[pieces.length - 2]); used = Double.parseDouble(pieces[2]); //get connected nodes if (used == 1) { currentIndividualConnectedNodes.add(Integer.parseInt(pieces[3])); currentIndividualConnectedNodes.add(Integer.parseInt(pieces[4])); } } currentIndividual.put(innovationNumber, new Pair<>(used, value)); } } reader.close(); data.add(currentPopulation); } //populate arrays double[][] usages = new double[generations][]; double[][] diversities = new double[generations][]; for (int g = 0; g < generations; g++) { usages[g] = new double[maxInnovationNumber]; diversities[g] = new double[maxInnovationNumber]; List<Map<Integer, Pair<Double, Double>>> currentPopulation = data.get(g); //populate usages double[][] values = new double[maxInnovationNumber][]; double[] localUsages = new double[maxInnovationNumber]; for (int i = 0; i < maxInnovationNumber; i++) { values[i] = new double[currentPopulation.size()]; } for (int p = 0; p < currentPopulation.size(); p++) { for (int i = 0; i < maxInnovationNumber; i++) { double value = 0; double used = 0; if (currentPopulation.get(p).containsKey(i)) { Pair<Double, Double> pair = currentPopulation.get(p).get(i); value = pair.getSecond(); used = pair.getFirst(); } values[i][p] = value; localUsages[i] = localUsages[i] + used; } } for (int i = 0; i < maxInnovationNumber; i++) { usages[g][i] = localUsages[i] / (double) currentPopulation.size(); diversities[g][i] = normalizedVar(values[i]); } } return new double[][][] { diversities, usages }; }
From source file:gov.noaa.pfel.coastwatch.util.FileVisitorDNLS.java
/** * This is a convenience method for using this class. * <p>This works with Amazon AWS S3 bucket URLs. Internal /'s in the keys will be * treated as folder separators. If there aren't any /'s, all the keys will * be in the root directory.//from www . j av a2 s . c om * * @param tDir The starting directory, with \\ or /, with or without trailing slash. * The resulting directoryPA will contain dirs with matching slashes and trailing slash. * @param tPathRegex a regex to constrain which subdirs to include. * This is ignored if recursive is false. * null or "" is treated as .* (i.e., match everything). * @param tDirectoriesToo if true, each directory name will get its own rows * in the results. * @return a table with columns with DIRECTORY, NAME, LASTMODIFIED, and SIZE columns. * LASTMODIFIED and SIZE are LongArrays -- For directories when the values * are otherwise unknown, the value will be Long.MAX_VALUE. * If directoriesToo=true, the original dir won't be included and any * directory's file NAME will be "". * @throws IOException if trouble */ public static Table oneStep(String tDir, String tFileNameRegex, boolean tRecursive, String tPathRegex, boolean tDirectoriesToo) throws IOException { long time = System.currentTimeMillis(); //is tDir an http URL? if (tDir.matches(FileVisitorDNLS.HTTP_REGEX)) { //Is it an S3 bucket with "files"? //If testing a "dir", url should have a trailing slash. Matcher matcher = AWS_S3_PATTERN.matcher(File2.addSlash(tDir)); //force trailing slash if (matcher.matches()) { //http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html //If files have file-system-like names, e.g., // http://bucketname.s3.amazonaws.com/dir1/dir2/fileName.ext) // http://nasanex.s3.amazonaws.com/NEX-DCP30/BCSD/rcp26/mon/atmos/tasmin/r1i1p1/v1.0/CONUS/tasmin_amon_BCSD_rcp26_r1i1p1_CONUS_NorESM1-M_209601-209912.nc // you still can't request just dir2 info because they aren't directories. // They are just object keys with internal slashes. //So specify prefix in request. Table table = makeEmptyTable(); StringArray directoryPA = (StringArray) table.getColumn(DIRECTORY); StringArray namePA = (StringArray) table.getColumn(NAME); LongArray lastModifiedPA = (LongArray) table.getColumn(LASTMODIFIED); LongArray sizePA = (LongArray) table.getColumn(SIZE); String bucketName = matcher.group(1); String prefix = matcher.group(2); String baseURL = tDir.substring(0, matcher.start(2)); AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); try { if (verbose) String2.log("FileVisitorDNLS.oneStep getting info from AWS S3 at" + "\nURL=" + tDir); //"\nbucket=" + bucketName + " prefix=" + prefix); //I wanted to generate lastMod for dir based on lastMod of files //but it would be inconsistent for different requests (recursive, fileNameRegex). //so just a set of dir names. HashSet<String> dirHashSet = new HashSet(); ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName) .withPrefix(prefix); ObjectListing objectListing; do { objectListing = s3client.listObjects(listObjectsRequest); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { String keyFullName = objectSummary.getKey(); String keyDir = File2.getDirectory(baseURL + keyFullName); String keyName = File2.getNameAndExtension(keyFullName); if (debugMode) String2.log( "keyFullName=" + keyFullName + "\nkeyDir=" + keyDir + "\n tDir=" + tDir); if (keyDir.startsWith(tDir) && //it should (tRecursive || keyDir.length() == tDir.length())) { //store this dir if (tDirectoriesToo) { //S3 only returns object keys. I must infer/collect directories. //Store this dir and parents back to tDir. String choppedKeyDir = keyDir; while (choppedKeyDir.length() >= tDir.length()) { if (!dirHashSet.add(choppedKeyDir)) break; //hash set already had this, so will already have parents //chop off last subdirectory choppedKeyDir = File2.getDirectory( choppedKeyDir.substring(0, choppedKeyDir.length() - 1)); //remove trailing / } } //store this file's information //Sometimes directories appear as files are named "" with size=0. //I don't store those as files. if (debugMode) String2.log("keyName=" + keyFullName + "\n tFileNameRegex=" + tFileNameRegex + " matches=" + keyName.matches(tFileNameRegex)); if (keyName.length() > 0 && keyName.matches(tFileNameRegex)) { directoryPA.add(keyDir); namePA.add(keyName); lastModifiedPA.add(objectSummary.getLastModified().getTime()); //epoch millis sizePA.add(objectSummary.getSize()); //long } } } listObjectsRequest.setMarker(objectListing.getNextMarker()); } while (objectListing.isTruncated()); //add directories to the table if (tDirectoriesToo) { Iterator<String> it = dirHashSet.iterator(); while (it.hasNext()) { directoryPA.add(it.next()); namePA.add(""); lastModifiedPA.add(Long.MAX_VALUE); sizePA.add(Long.MAX_VALUE); } } table.leftToRightSortIgnoreCase(2); return table; } catch (AmazonServiceException ase) { throw new IOException("AmazonServiceException: " + ase.getErrorType() + " ERROR, HTTP Code=" + ase.getStatusCode() + ": " + ase.getMessage(), ase); } catch (AmazonClientException ace) { throw new IOException(ace.getMessage(), ace); } } //HYRAX before THREDDS //http://dods.jpl.nasa.gov/opendap/ocean_wind/ccmp/L3.5a/data/flk/1988/ matcher = HYRAX_PATTERN.matcher(tDir); if (matcher.matches()) { try { if (verbose) String2.log("FileVisitorDNLS.oneStep getting info from Hyrax at" + "\nURL=" + tDir); Table table = makeEmptyTable(); StringArray directoryPA = (StringArray) table.getColumn(DIRECTORY); StringArray namePA = (StringArray) table.getColumn(NAME); LongArray lastModifiedPA = (LongArray) table.getColumn(LASTMODIFIED); LongArray sizePA = (LongArray) table.getColumn(SIZE); DoubleArray lastModDA = new DoubleArray(); addToHyraxUrlList(tDir, tFileNameRegex, tRecursive, tPathRegex, tDirectoriesToo, namePA, lastModDA, sizePA); lastModifiedPA.append(lastModDA); int n = namePA.size(); for (int i = 0; i < n; i++) { String fn = namePA.get(i); directoryPA.add(File2.getDirectory(fn)); namePA.set(i, File2.getNameAndExtension(fn)); } table.leftToRightSortIgnoreCase(2); return table; } catch (Throwable t) { throw new IOException(t.getMessage(), t); } } //THREDDS matcher = THREDDS_PATTERN.matcher(tDir); if (matcher.matches()) { try { if (verbose) String2.log("FileVisitorDNLS.oneStep getting info from THREDDS at" + "\nURL=" + tDir); Table table = makeEmptyTable(); StringArray directoryPA = (StringArray) table.getColumn(DIRECTORY); StringArray namePA = (StringArray) table.getColumn(NAME); LongArray lastModifiedPA = (LongArray) table.getColumn(LASTMODIFIED); LongArray sizePA = (LongArray) table.getColumn(SIZE); DoubleArray lastModDA = new DoubleArray(); addToThreddsUrlList(tDir, tFileNameRegex, tRecursive, tPathRegex, tDirectoriesToo, namePA, lastModDA, sizePA); lastModifiedPA.append(lastModDA); int n = namePA.size(); for (int i = 0; i < n; i++) { String fn = namePA.get(i); directoryPA.add(File2.getDirectory(fn)); namePA.set(i, File2.getNameAndExtension(fn)); } table.leftToRightSortIgnoreCase(2); return table; } catch (Throwable t) { throw new IOException(t.getMessage(), t); } } //default: Apache-style WAF try { if (verbose) String2.log("FileVisitorDNLS.oneStep getting info from Apache-style WAF at" + "\nURL=" + tDir); Table table = makeEmptyTable(); StringArray directorySA = (StringArray) table.getColumn(DIRECTORY); StringArray nameSA = (StringArray) table.getColumn(NAME); LongArray lastModLA = (LongArray) table.getColumn(LASTMODIFIED); LongArray sizeLA = (LongArray) table.getColumn(SIZE); addToWAFUrlList(tDir, tFileNameRegex, tRecursive, tPathRegex, tDirectoriesToo, directorySA, nameSA, lastModLA, sizeLA); table.leftToRightSortIgnoreCase(2); return table; } catch (Throwable t) { throw new IOException(t.getMessage(), t); } } //local files //follow symbolic links: https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html //But this doesn't follow Windows symbolic link .lnk's: // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4237760 FileVisitorDNLS fv = new FileVisitorDNLS(tDir, tFileNameRegex, tRecursive, tPathRegex, tDirectoriesToo); EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); Files.walkFileTree(FileSystems.getDefault().getPath(tDir), opts, //follow symbolic links Integer.MAX_VALUE, //maxDepth fv); fv.table.leftToRightSortIgnoreCase(2); if (verbose) String2.log("FileVisitorDNLS.oneStep(local) finished successfully. n=" + fv.directoryPA.size() + " time=" + (System.currentTimeMillis() - time) + "ms"); return fv.table; }
From source file:edu.purdue.cybercenter.dm.storage.GlobusStorageFileManager.java
private List<String> createSymbolicLinks(List<StorageFile> storageFiles) throws IOException { List<String> fileLinks = new ArrayList<>(); for (StorageFile storageFile : storageFiles) { String dest = getAbsolutePath(storageFile); Path destPath = FileSystems.getDefault().getPath(dest); File destFile = destPath.toFile(); if (!destFile.exists()) { destFile.getParentFile().mkdirs(); destFile.createNewFile();// w ww . ja va2 s. co m } String location = storageFile.getLocation(); Path destLink = FileSystems.getDefault().getPath("../../" + location); String link = getWorkareaDirectory() + GLOBUS + destLink.toFile().getName(); Path linkPath = FileSystems.getDefault().getPath(link); Files.createSymbolicLink(linkPath, destLink); fileLinks.add(link); } return fileLinks; }
From source file:com.clust4j.data.TestDataSet.java
@Test public void testWrite1() throws IOException { String path = "iris.csv"; final File file = new File(path); Path ppath = FileSystems.getDefault().getPath(path); try {// www. j av a 2 s . c o m TestSuite.IRIS_DATASET.toFlatFile(true, file); } finally { Files.delete(ppath); } }
From source file:de.prozesskraft.pkraft.Manager.java
/** * erstellt fuer jeden running step einen watchkey * es soll jedes stepverzeichnis mit dem status 'working' observiert werden bis das file ".exit" erscheint * @param process/*from www.j a v a 2s. c o m*/ * @throws IOException */ private static void createWatchKeysForAllRunningSteps(Process process) throws IOException { // diesen Thread ablegen, damit er vom zyklischen thread gekillt werden kann watcherThread = Thread.currentThread(); // einen neuen map erzeugen fuer die watchKeys keys = new HashMap<WatchKey, Path>(); WatchService watcher = FileSystems.getDefault().newWatchService(); // Anlegen des WatchKeys fuer den Prozess (falls er gestoppt wird, erfolgt die Komunikation mit diesem manager ueber das binaerfile) Path processDir = Paths.get(process.getRootdir()); System.err.println("info: creating a watchkey for the process path " + process.getRootdir()); WatchKey keyProcess = processDir.register(watcher, ENTRY_MODIFY); keys.put(keyProcess, processDir); // Anlegen der WatchKeys fuer jeden laufenden Step for (Step actStep : process.getStep()) { if (actStep.getStatus().equals("working")) { Path stepDir = Paths.get(actStep.getAbsdir()); try { System.err.println("info: step " + actStep.getName() + " is working -> creating a watchkey for its path " + actStep.getAbsdir()); System.err.println("debug: creating..."); WatchKey key = stepDir.register(watcher, ENTRY_CREATE); System.err.println("debug: creating...done. putting to the map"); keys.put(key, stepDir); System.err.println("debug: creating...done. putting to the map...done"); } catch (IOException e) { System.err.println(e); } catch (Exception e) { System.err.println(e); } java.io.File stepDirExitFile = new java.io.File(actStep.getAbsdir() + "/.exit"); java.io.File stepDirStatusFile = new java.io.File(actStep.getAbsdir() + "/.status"); // falls die datei bereits existiert, wird sofort erneut der Prozess weitergeschoben // dies ist dann der fall, wenn ein step gestartet wurde, und danach der manager neu gestartet wurde if (stepDirExitFile.exists()) { System.err.println("info: .exit file already exists -> shortcutting to pushing the process"); // alle keys loeschen keys = null; // den prozess weiter pushen pushProcessAsFarAsPossible(process.getRootdir() + "/process.pmb", false); } // falls der step ein process ist, bibts dort kein .exit file sondern ein .status file else if (stepDirStatusFile.exists()) { System.err.println("info: .status file already exists."); try { java.util.List<String> statusInhalt = Files.readAllLines(stepDirStatusFile.toPath(), Charset.defaultCharset()); if (statusInhalt.size() > 0) { String firstLine = statusInhalt.get(0); System.err.println("info: status changed to: " + firstLine); System.err.println("info: .status file contains status " + firstLine); // wenn ein finaler status, dann soll manager aufgeweckt werden if (firstLine.equals("error") || firstLine.equals("finished")) { System.err.println("info: --> shortcutting to pushing process"); // alle keys loeschen keys = null; // den prozess weiter pushen pushProcessAsFarAsPossible(process.getRootdir() + "/process.pmb", false); } } } catch (IOException e) { // TODO Auto-generated catch block System.err.println( "IOException: trying to read file: " + stepDirStatusFile.getAbsolutePath()); e.printStackTrace(); } catch (ExceptionInInitializerError e) { System.err.println("ExceptionInInitializerError: trying to read file: " + stepDirStatusFile.getAbsolutePath()); e.printStackTrace(); } } } } process.log("info", "now into the watchloop"); // warten auf ein Signal von einem WatchKey for (;;) { WatchKey key; try { key = watcher.take(); } catch (InterruptedException e) { System.err.println(new Timestamp(System.currentTimeMillis()) + ": ---- watcher thread: interrupted! returning to alternativer Thread"); return; } Path dir = keys.get(key); if (dir == null) { System.err.println("WatchKey not recognized!!"); continue; } for (WatchEvent<?> event : key.pollEvents()) { // System.err.println("debug: poll event " + event); WatchEvent.Kind kind = event.kind(); WatchEvent<Path> ev = (WatchEvent<Path>) event; Path name = ev.context(); // dieses logging fuehrt zur aenderung von stderr.txt und .log, was wiederum ein ENTRY_MODIFY ausloest etc. endlosschleife bis platte volllaeuft // System.err.println("debug: poll context " + name); Path child = dir.resolve(name); // System.err.println("debug: poll child " + child); if (kind == ENTRY_CREATE) { if (child.endsWith(".exit")) { System.err.println("info: waking up, because file created: " + child.toString()); // alle keys loeschen keys = null; // den prozess weiter pushen pushProcessAsFarAsPossible(process.getRootdir() + "/process.pmb", false); } } if ((kind == ENTRY_MODIFY) && (child.endsWith("process.pmb"))) { // System.err.println("info: waking up, because process binary file has been modified: " + child.toString()); // alle keys loeschen keys = null; // den prozess weiter pushen pushProcessAsFarAsPossible(process.getRootdir() + "/process.pmb", false); } if (kind == ENTRY_CREATE || kind == ENTRY_MODIFY) { if (child.endsWith(".status")) { try { java.util.List<String> statusInhalt = Files.readAllLines(child, Charset.defaultCharset()); if (statusInhalt.size() > 0) { String firstLine = statusInhalt.get(0); System.err.println("info: status changed to: " + firstLine); // wenn ein finaler status, dann soll manager aufgeweckt werden if (firstLine.equals("error") || firstLine.equals("finished")) { System.err.println("info: waking up, because status changed to: " + firstLine); // alle keys loeschen keys = null; // den prozess weiter pushen pushProcessAsFarAsPossible(process.getRootdir() + "/process.pmb", false); } } } catch (IOException e) { // TODO Auto-generated catch block System.err.println("IOException: trying to read file: " + child.toString()); e.printStackTrace(); } catch (ExceptionInInitializerError e) { System.err.println( "ExceptionInInitializerError: trying to read file: " + child.toString()); e.printStackTrace(); } } } // reset the triggered key key.reset(); } } }
From source file:com.clust4j.data.TestDataSet.java
@Test public void testWrite2() throws IOException { String path = "iris.csv"; final File file = new File(path); Path ppath = FileSystems.getDefault().getPath(path); try {// ww w . j av a 2 s .co m TestSuite.IRIS_DATASET.toFlatFile(false, file); } finally { Files.delete(ppath); } }
From source file:edu.purdue.cybercenter.dm.storage.GlobusStorageFileManager.java
private void removeSymbolicLinks(List<String> fileLinks) throws IOException { for (String fileLink : fileLinks) { Path linkPath = FileSystems.getDefault().getPath(fileLink); //if (Files.isSymbolicLink(linkPath)) { Files.delete(linkPath);//from ww w . java 2 s. co m //} } }
From source file:com.clust4j.data.TestDataSet.java
@Test public void testWrite3() throws IOException { String path = "iris.csv"; final File file = new File(path); Path ppath = FileSystems.getDefault().getPath(path); try {/*w ww. j a v a 2s. c om*/ TestSuite.IRIS_DATASET.toFlatFile(true, file, '|'); } finally { Files.delete(ppath); } }
From source file:com.clust4j.data.TestDataSet.java
@Test public void testWrite4() throws IOException { String path = "iris.csv"; final File file = new File(path); Path ppath = FileSystems.getDefault().getPath(path); try {/*from w w w .ja va 2s . co m*/ TestSuite.IRIS_DATASET.toFlatFile(false, file, '|'); } finally { Files.delete(ppath); } }