List of usage examples for java.io FileInputStream FileInputStream
public FileInputStream(FileDescriptor fdObj)
FileInputStream
by using the file descriptor fdObj
, which represents an existing connection to an actual file in the file system. From source file:MainClass.java
public static void main(String args[]) { FileInputStream fIn;//from ww w. j a v a 2 s . co m FileOutputStream fOut; FileChannel fIChan, fOChan; long fSize; MappedByteBuffer mBuf; try { fIn = new FileInputStream(args[0]); fOut = new FileOutputStream(args[1]); fIChan = fIn.getChannel(); fOChan = fOut.getChannel(); fSize = fIChan.size(); mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize); fOChan.write(mBuf); // this copies the file fIChan.close(); fIn.close(); fOChan.close(); fOut.close(); } catch (IOException exc) { System.out.println(exc); System.exit(1); } catch (ArrayIndexOutOfBoundsException exc) { System.out.println("Usage: Copy from to"); System.exit(1); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { File file = new File("image.gif"); System.out.println(getFormatName(file)); InputStream is = new FileInputStream(file); is.close();//from ww w .j a va 2s .c om System.out.println(getFormatName(is)); }
From source file:Main.java
public static void main(String[] args) throws Exception { String destinationname = "d:\\"; byte[] buf = new byte[1024]; ZipInputStream zipinputstream = null; ZipEntry zipentry;//from w ww. j av a 2 s . c om zipinputstream = new ZipInputStream(new FileInputStream("fileName")); zipentry = zipinputstream.getNextEntry(); while (zipentry != null) { String entryName = zipentry.getName(); FileOutputStream fileoutputstream; File newFile = new File(entryName); String directory = newFile.getParent(); if (directory == null) { if (newFile.isDirectory()) break; } fileoutputstream = new FileOutputStream(destinationname + entryName); int n; while ((n = zipinputstream.read(buf, 0, 1024)) > -1) { fileoutputstream.write(buf, 0, n); } fileoutputstream.close(); zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); } zipinputstream.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("test.xml"))); int eventTypeID = reader.nextTag(); reader.require(XMLStreamConstants.START_ELEMENT, null, "person"); eventTypeID = reader.nextTag();// w ww. j a v a 2 s . c o m try { reader.require(XMLStreamConstants.START_ELEMENT, null, "first_name"); } catch (XMLStreamException e) { System.out.println("Assertion failed. " + e.getMessage() + " at " + reader.getLocation().getLineNumber() + ":" + reader.getLocation().getColumnNumber()); } System.out.println(reader.getElementText()); }
From source file:BufferConverter.java
public static void main(String[] arguments) { try {/*w w w . j a v a2 s . c o m*/ String data = "friends.dat"; FileInputStream inData = new FileInputStream(data); FileChannel inChannel = inData.getChannel(); long inSize = inChannel.size(); ByteBuffer source = ByteBuffer.allocate((int) inSize); inChannel.read(source, 0); source.position(0); for (int i = 0; source.remaining() > 0; i++) System.out.print(source.get() + " "); source.position(0); Charset ascii = Charset.forName("US-ASCII"); CharsetDecoder toAscii = ascii.newDecoder(); CharBuffer destination = toAscii.decode(source); destination.position(0); System.out.println("\n\nNew character data:"); for (int i = 0; destination.remaining() > 0; i++) System.out.print(destination.get()); } catch (Exception ioe) { System.out.println(ioe.getMessage()); } }
From source file:Main.java
public static void main(String[] args) throws Exception { JAXBContext context = JAXBContext.newInstance(User.class); XMLInputFactory xif = XMLInputFactory.newInstance(); FileInputStream fis = new FileInputStream("input.xml"); XMLStreamReader xsr = xif.createXMLStreamReader(fis); xsr.nextTag();/*from w w w .ja va2 s . c o m*/ String noNamespaceSchemaLocation = xsr.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "noNamespaceSchemaLocation"); System.out.println(noNamespaceSchemaLocation); Unmarshaller um = context.createUnmarshaller(); User response = (User) um.unmarshal(xsr); }
From source file:FileIOApp.java
public static void main(String args[]) throws IOException { FileOutputStream outStream = new FileOutputStream("test.txt"); String s = "This is a test."; for (int i = 0; i < s.length(); ++i) outStream.write(s.charAt(i));/*from w w w . j a v a2 s . c om*/ outStream.close(); FileInputStream inStream = new FileInputStream("test.txt"); int inBytes = inStream.available(); System.out.println("inStream has " + inBytes + " available bytes"); byte inBuf[] = new byte[inBytes]; int bytesRead = inStream.read(inBuf, 0, inBytes); System.out.println(bytesRead + " bytes were read"); System.out.println("They are: " + new String(inBuf)); inStream.close(); File f = new File("test.txt"); f.delete(); }
From source file:Main.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document doc = docBuilder.parse(new FileInputStream("data.xml")); Element root = doc.getDocumentElement(); org.w3c.dom.NodeList nodeList = root.getElementsByTagName("key"); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(((Node) nodeList.item(i)).getAttributes().getNamedItem("keyname")); System.out.println("\tvalue: " + ((Node) nodeList.item(i)).getTextContent()); }//from w ww . j av a2s . c o m }
From source file:MainClass.java
public static void main(String[] args) throws Exception { FileChannel fc = new FileOutputStream("data2.txt").getChannel(); ByteBuffer buff = ByteBuffer.allocate(24); // More than needed buff.asCharBuffer().put("Some text"); fc.write(buff);//from w w w.java 2 s . c om fc.close(); fc = new FileInputStream("data2.txt").getChannel(); buff.clear(); fc.read(buff); buff.flip(); System.out.println(buff.asCharBuffer()); }
From source file:Main.java
public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.put("Chapter Count", "200"); prop.put("Tutorial Count", "15"); // create a output and input as a xml file FileOutputStream fos = new FileOutputStream("properties.xml"); FileInputStream fis = new FileInputStream("properties.xml"); // store the properties in the specific xml prop.storeToXML(fos, null);// w ww . j av a2s . c o m // load from the xml that we saved earlier prop.loadFromXML(fis); // print the properties list prop.list(System.out); }