Coder Social home page Coder Social logo

java-io-1's Introduction

java-io

Java IO

#IOUtils

maven dependency:

 <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.5</version>
  </dependency>
  

//We can create an InputStream object from a String as follows
InputStream is=IOUtils.toInputStream("This is a String","utf-8");

//We can copy the contents of an InputStream object to an OutputStream object as follows
OutputStream o=new FileOutputStream("abc.txt");
int bytes=IOUtils.copy(is, o);
out.println("File Written with "+bytes+" bytes");
IOUtils.closeQuietly(o);

//We can convert a Reader object to a String using IOUtils.toString(Reader r) as below
FileReader fr3=new FileReader("abc.txt");
String st=IOUtils.toString(fr3);

//We can get the contents of an InputStream as a byte array as below
FileInputStream fin=new FileInputStream("abc.txt");
byte[] buf=new byte[100]
int len=IOUtils.read(fin,buf);
out.println("The Length of Input Stream : "+len);


//We can get the contents of an InputStream as a list of Strings as below
FileInputStream fin=new FileInputStream("abc.txt");
List ls=IOUtils.readLines(fin,"utf-8");

//We write lines from a collection to a Writer object as follows
Writer swr=new StringWriter();
IOUtils.writeLines(ls, IOUtils.LINE_SEPARATOR_WINDOWS, swr);   

//We can get the LineIterator for an InputStream as below
FileInputStream fin=new FileInputStream("D:\\Java_Workspace\\IOUtilsExample\\abc.txt");
LineIterator lt=IOUtils.lineIterator(fin, "utf-8");
while(lt.hasNext())
{
       String line=lt.nextLine();
       //Perform the Regular Processing with the String
}
//When you are done you have to close either the LineIterator object
lt.close();
//or the InputStream / Reader object.



#Load Resource as Stream

//load resource as string
//final InputStream resourceAsStream = WorkingWithFile.class.getResourceAsStream("config.properties");
final InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
System.out.println(IOUtils.toString(resourceAsStream, Charset.defaultCharset()));

Java System Properties

- JRE related system properties

java.home    JRE home directory, e.g., “C:\Program Files\Java\jdk1.7.0_09\jre”.
java.library.path   JRE library search path for search native libraries. It is usually but not necessarily taken from the environment variable PATH.
java.class.path JRE CLASSPATH, e.g., . (for current working directory).
java.ext.dirs   JRE extension library path(s), e.g, “C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext;C:\Windows\Sun\Java\lib\ext”.
java.version    JRE version, e.g., 1.7.0_09.
java.runtime.version    JRE version, e.g. 1.7.0_09-b05.

- File related system properties

file.separator  symbol for file directory separator such as d:\test\test.java. The default is \ for windows or / for Unix/Mac.
path.separator  symbol for separating path entries, e.g., in PATH or CLASSPATH. The default is ; for windows or : for Unix/Mac.
line.separator  symbol for end-of-line (or new line). The default is “\r\n” for windows or “\n” for Unix/Mac OS X.

- User related system properties

user.name   the user’s name.
user.home   the user’s home directory.
user.dir    the user’s current working directory.

- OS related system properties

os.name the OS’s name, e.g., “Windows 7”.
os.version  the OS’s version, e.g., “6.1”.
os.arch the OS’s architecture, e.g., “x86”.

java-io-1's People

Contributors

sophea avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.