Java Code Examples for org.apache.commons.io.FileSystemUtils

The following examples show how to use org.apache.commons.io.FileSystemUtils. These examples are extracted from open source projects. 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
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 Project: gemfirexd-oss   Source File: ExtractorUtils.java    License: 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 Project: gemfirexd-oss   Source File: ExtractorUtils.java    License: 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 Project: restcommander   Source File: MonitorProvider.java    License: 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 Project: freeacs   Source File: DiskSpaceCheck.java    License: 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 Project: gemfirexd-oss   Source File: ExtractorUtils.java    License: 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 Project: gemfirexd-oss   Source File: ExtractorUtils.java    License: 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 Project: tutorials   Source File: CommonsIOUnitTest.java    License: MIT License 4 votes vote down vote up
@Test
public void whenUsingFileSystemUtils_thenDriveFreeSpace() throws IOException {

    long freeSpace = FileSystemUtils.freeSpaceKb("/");
}
 
Example 9
Source Project: levelup-java-examples   Source File: GetAvailableSpace.java    License: 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);
}