org.apache.commons.io.FileSystemUtils Java Examples

The following examples show how to use org.apache.commons.io.FileSystemUtils. 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: CommonsExample.java    From pragmatic-java-engineer with GNU General Public License v3.0 6 votes vote down vote up
public static void io() throws IOException {
    File file = new File("/data/data.txt");
    List lines = FileUtils.readLines(file, "UTF-8"); //读取成字符串结合
    System.out.println(lines);

    byte[] fileBytes = FileUtils.readFileToByteArray(file); //读取成字节数组
    FileUtils.writeByteArrayToFile(file, fileBytes); //字节写入文件
    FileUtils.writeStringToFile(file, "test"); //字符串写入文件

    InputStream is = new URL("http://baidu.cim").openStream();
    try {
        System.out.println(IOUtils.toString(is, "utf-8"));
        //IOUtils.readLines(is, "utf-8");
    } finally {
        IOUtils.closeQuietly(is);
    }

    FileSystemUtils.freeSpaceKb();
}
 
Example #2
Source File: ExtractorUtils.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/***
 * Determines if the target directory for extraction has sufficient space to store the output of data extraction
 * @param serverInfoMap
 * @param targetDirectory
 * @return True if sufficient space , false otherwise
 * @throws IOException
 */
public static boolean checkDiskSpaceInTargetDirectory(Map<String, ServerInfo> serverInfoMap, String targetDirectory) throws IOException {
  long totalDiskDirSize = getTotalSize(serverInfoMap);
  long totalSpaceAvailable = FileSystemUtils.freeSpaceKb(targetDirectory);

  GemFireXDDataExtractorImpl.logInfo("Total size of data to be extracted : " +  (double)totalDiskDirSize/1024d +  "MB");
  GemFireXDDataExtractorImpl.logInfo("Disk space available in the output directory : " + (double)totalSpaceAvailable/1024d + "MB");

  if (totalSpaceAvailable < totalDiskDirSize) {
    if ("n".equalsIgnoreCase(getUserInput())) {
      return false;
    }
  } else {
    GemFireXDDataExtractorImpl.logInfo("Sufficient disk space to carry out data extraction");
  }
  return true;
}
 
Example #3
Source File: ExtractorUtils.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/***
 * Determines if the target directory for extraction has sufficient space to store the output of data extraction
 * @param serverInfoMap
 * @param targetDirectory
 * @return True if sufficient space , false otherwise
 * @throws IOException
 */
public static boolean checkDiskSpaceInTargetDirectory(Map<String, ServerInfo> serverInfoMap, String targetDirectory) throws IOException {
  long totalDiskDirSize = getTotalSize(serverInfoMap);
  long totalSpaceAvailable = FileSystemUtils.freeSpaceKb(targetDirectory);

  GemFireXDDataExtractorImpl.logInfo("Total size of data to be extracted : " +  (double)totalDiskDirSize/1024d +  "MB");
  GemFireXDDataExtractorImpl.logInfo("Disk space available in the output directory : " + (double)totalSpaceAvailable/1024d + "MB");

  if (totalSpaceAvailable < totalDiskDirSize) {
    if ("n".equalsIgnoreCase(getUserInput())) {
      return false;
    }
  } else {
    GemFireXDDataExtractorImpl.logInfo("Sufficient disk space to carry out data extraction");
  }
  return true;
}
 
Example #4
Source File: MonitorProvider.java    From restcommander with Apache License 2.0 6 votes vote down vote up
public DiskUsage getFreeDiskspace() {
	
	long freeSpace = -1L;
	try {
		freeSpace = FileSystemUtils.freeSpaceKb("/");
	} catch (IOException e) {
		models.utils.LogUtils.printLogError("Error in getFreeDiskspace() " + e.getLocalizedMessage());
		//e.printStackTrace();
	}
	int gb = 1024*1024;
	DiskUsage usage = new DiskUsage();
	usage.freeSpaceGb = (double)freeSpace/ (double)gb;
	
	if(VarUtils.IN_DETAIL_DEBUG){
		
		models.utils.LogUtils.printLogNormal("Free Space:" + usage.freeSpaceGb + " GB");
	}
	
	currentDiskUsage = usage;
	return usage;
}
 
Example #5
Source File: DiskSpaceCheck.java    From freeacs with MIT License 5 votes vote down vote up
@Override
public void runImpl() throws Throwable {
  freeSpace = FileSystemUtils.freeSpaceKb(new File(".").getAbsolutePath());
  if (freeSpace < getMinFreeDiskSpace()) {
    logger.error("Server will pause, since free disk space is " + freeSpace / 1024 + " MB.");
    SyslogServer.pause(true);
    Syslog2DB.pause(true);
  } else if (SyslogServer.isPause() && freeSpace >= getMinFreeDiskSpace()) {
    logger.info("Server will resume operation, free disk space is " + freeSpace / 1024 + " MB.");
    SyslogServer.pause(false);
    Syslog2DB.pause(false);
  }
}
 
Example #6
Source File: ExtractorUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected static boolean checkDiskSpaceInTargetDirectory(String targetDirectory) throws IOException {
  long totalDiskDirSize = getTotalSize(targetDirectory);
  long totalSpaceAvailable = FileSystemUtils.freeSpaceKb(targetDirectory);
  GemFireXDDataExtractorImpl.logInfo("Estimated data to be extracted : " +  (double)totalDiskDirSize/1024d +  "MB");
  GemFireXDDataExtractorImpl.logInfo ("Disk space available in the output directory : " + (double)totalSpaceAvailable/1024d + "MB");
  if (totalSpaceAvailable < totalDiskDirSize) {
    if ("n".equalsIgnoreCase(getUserInput())) {
      return false;
    }
  } else {
    GemFireXDDataExtractorImpl.logInfo("Sufficient disk space to carry out data extraction");
  }
  return true;
}
 
Example #7
Source File: ExtractorUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected static boolean checkDiskSpaceInTargetDirectory(String targetDirectory) throws IOException {
  long totalDiskDirSize = getTotalSize(targetDirectory);
  long totalSpaceAvailable = FileSystemUtils.freeSpaceKb(targetDirectory);
  GemFireXDDataExtractorImpl.logInfo("Estimated data to be extracted : " +  (double)totalDiskDirSize/1024d +  "MB");
  GemFireXDDataExtractorImpl.logInfo ("Disk space available in the output directory : " + (double)totalSpaceAvailable/1024d + "MB");
  if (totalSpaceAvailable < totalDiskDirSize) {
    if ("n".equalsIgnoreCase(getUserInput())) {
      return false;
    }
  } else {
    GemFireXDDataExtractorImpl.logInfo("Sufficient disk space to carry out data extraction");
  }
  return true;
}
 
Example #8
Source File: CommonsIOUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenUsingFileSystemUtils_thenDriveFreeSpace() throws IOException {

    long freeSpace = FileSystemUtils.freeSpaceKb("/");
}
 
Example #9
Source File: GetAvailableSpace.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void get_available_space_apache_commons () throws IOException {
	
	long freeSpace = FileSystemUtils.freeSpaceKb("/");
	
	assertTrue(freeSpace > 0);
}