List of usage examples for java.io FileNotFoundException printStackTrace
public void printStackTrace(PrintStream s)
From source file:MainClass.java
public static void main(String[] args) { String phrase = new String("www.java2s.com API \n"); File aFile = new File("test.txt"); FileOutputStream file = null; try {//from w w w.j a va 2 s . co m file = new FileOutputStream(aFile, true); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel outChannel = file.getChannel(); ByteBuffer buf = ByteBuffer.allocate(phrase.length()); byte[] bytes = phrase.getBytes(); buf.put(bytes); buf.flip(); try { outChannel.write(buf); file.close(); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("file.dat"); FileInputStream inFile = null; try {/*w w w.j av a 2 s . c o m*/ inFile = new FileInputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); System.exit(0); } FileChannel inChannel = inFile.getChannel(); final int COUNT = 6; ByteBuffer buf = ByteBuffer.allocate(8 * COUNT); long[] data = new long[COUNT]; try { while (inChannel.read(buf) != -1) { ((ByteBuffer) (buf.flip())).asLongBuffer().get(data); System.out.println(); for (long prime : data) System.out.printf("%10d", prime); buf.clear(); } System.out.println("\nEOF reached."); inFile.close(); } catch (IOException e) { e.printStackTrace(System.err); System.exit(1); } }
From source file:MainClass.java
public static void main(String[] args) { String phrase = new String("text \n"); String dirname = "C:/test"; String filename = "charData.txt"; File dir = new File(dirname); File aFile = new File(dir, filename); FileOutputStream outputFile = null; try {//from w w w .j a v a 2 s . c o m outputFile = new FileOutputStream(aFile, true); System.out.println("File stream created successfully."); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println("New buffer: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); for (char ch : phrase.toCharArray()) { buf.putChar(ch); } System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); buf.flip(); System.out.println("Buffer after flip: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); try { outChannel.write(buf); outputFile.close(); System.out.println("Buffer contents written to file."); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("primes.txt"); FileInputStream inFile = null; try {//from w w w . ja v a 2 s. c o m inFile = new FileInputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel inChannel = inFile.getChannel(); try { ByteBuffer lengthBuf = ByteBuffer.allocate(8); while (true) { if (inChannel.read(lengthBuf) == -1) { break; } lengthBuf.flip(); int strLength = (int) lengthBuf.getDouble(); ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8); if (inChannel.read(buf) == -1) { break; } buf.flip(); byte[] strChars = new byte[2 * strLength]; buf.get(strChars); System.out.println(strLength); System.out.println(ByteBuffer.wrap(strChars).asCharBuffer()); System.out.println(buf.getLong()); lengthBuf.clear(); } inFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:MainClass.java
public static void main(String[] args) { long[] primes = new long[] { 1, 2, 3, 5, 7 }; File aFile = new File("C:/test/primes.bin"); FileOutputStream outputFile = null; try {/*from w w w .j a v a 2s .co m*/ outputFile = new FileOutputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel file = outputFile.getChannel(); final int BUFFERSIZE = 100; ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE); LongBuffer longBuf = buf.asLongBuffer(); int primesWritten = 0; while (primesWritten < primes.length) { longBuf.put(primes, primesWritten, min(longBuf.capacity(), primes.length - primesWritten)); buf.limit(8 * longBuf.position()); try { file.write(buf); primesWritten += longBuf.position(); } catch (IOException e) { e.printStackTrace(System.err); } longBuf.clear(); buf.clear(); } try { System.out.println("File written is " + file.size() + "bytes."); outputFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("data.dat"); FileInputStream inFile = null; try {//ww w . java 2 s . co m inFile = new FileInputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); System.exit(1); } FileChannel inChannel = inFile.getChannel(); final int COUNT = 6; ByteBuffer buf = ByteBuffer.allocate(8 * COUNT); long[] data = new long[COUNT]; try { int pos = 0; while (inChannel.read(buf) != -1) { try { ((ByteBuffer) (buf.flip())).asLongBuffer().get(data); pos = data.length; } catch (BufferUnderflowException e) { LongBuffer longBuf = buf.asLongBuffer(); pos = longBuf.remaining(); longBuf.get(data, 0, pos); } System.out.println(); for (int i = 0; i < pos; i++) { System.out.printf("%10d", data[i]); } buf.clear(); } System.out.println("\nEOF reached."); inFile.close(); } catch (IOException e) { e.printStackTrace(System.err); System.exit(1); } }
From source file:MainClass.java
public static void main(String[] args) { String phrase = new String("www.java2s.com\n"); File aFile = new File("test.txt"); FileOutputStream outputFile = null; try {//from ww w. j av a 2s . co m outputFile = new FileOutputStream(aFile, true); System.out.println("File stream created successfully."); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println("New buffer: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); // Load the data into the buffer for (char ch : phrase.toCharArray()) { buf.putChar(ch); } System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); buf.flip(); System.out.println("Buffer after flip: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); try { outChannel.write(buf); outputFile.close(); System.out.println("Buffer contents written to file."); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:MainClass.java
public static void main(String[] args) { long[] primes = new long[] { 1, 2, 3, 5, 7 }; File aFile = new File("C:/test/primes.txt"); FileOutputStream outputFile = null; try {//from ww w . j a v a 2s .c o m outputFile = new FileOutputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel file = outputFile.getChannel(); final int BUFFERSIZE = 100; ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE); DoubleBuffer doubleBuf = buf.asDoubleBuffer(); buf.position(8); CharBuffer charBuf = buf.asCharBuffer(); for (long prime : primes) { String primeStr = "prime = " + prime; doubleBuf.put(0, (double) primeStr.length()); charBuf.put(primeStr); buf.position(2 * charBuf.position() + 8); LongBuffer longBuf = buf.asLongBuffer(); longBuf.put(prime); buf.position(buf.position() + 8); buf.flip(); try { file.write(buf); } catch (IOException e) { e.printStackTrace(System.err); } buf.clear(); doubleBuf.clear(); charBuf.clear(); } try { System.out.println("File written is " + file.size() + "bytes."); outputFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:MainClass.java
public static void main(String[] args) { int count = 100; long[] numbers = new long[count]; for (int i = 0; i < numbers.length; i++) { numbers[i] = i;/* w ww . j a v a 2 s . com*/ } File aFile = new File("data.bin"); FileOutputStream outputFile = null; try { outputFile = new FileOutputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); System.exit(1); } FileChannel file = outputFile.getChannel(); final int BUFFERSIZE = 100; ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE); LongBuffer longBuf = buf.asLongBuffer(); int numberWritten = 0; while (numberWritten < numbers.length) { longBuf.put(numbers, numberWritten, min(longBuf.capacity(), numbers.length - numberWritten)); buf.limit(8 * longBuf.position()); try { file.write(buf); numberWritten += longBuf.position(); } catch (IOException e) { e.printStackTrace(System.err); System.exit(1); } longBuf.clear(); buf.clear(); } try { System.out.println("File written is " + file.size() + " bytes."); outputFile.close(); } catch (IOException e) { e.printStackTrace(System.err); System.exit(1); } }
From source file:MainClass.java
public static void main(String[] args) { String[] phrases = { "A", "B 1", "C 1.3" }; String dirname = "C:/test"; String filename = "Phrases.txt"; File dir = new File(dirname); File aFile = new File(dir, filename); FileOutputStream outputFile = null; try {/* w w w . j a v a2s . com*/ outputFile = new FileOutputStream(aFile, true); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println(buf.position()); System.out.println(buf.limit()); System.out.println(buf.capacity()); CharBuffer charBuf = buf.asCharBuffer(); System.out.println(charBuf.position()); System.out.println(charBuf.limit()); System.out.println(charBuf.capacity()); Formatter formatter = new Formatter(charBuf); int number = 0; for (String phrase : phrases) { formatter.format("%n %s", ++number, phrase); System.out.println(charBuf.position()); System.out.println(charBuf.limit()); System.out.println(charBuf.capacity()); charBuf.flip(); System.out.println(charBuf.position()); System.out.println(charBuf.limit()); System.out.println(charBuf.length()); buf.limit(2 * charBuf.length()); // Set byte buffer limit System.out.println(buf.position()); System.out.println(buf.limit()); System.out.println(buf.remaining()); try { outChannel.write(buf); buf.clear(); charBuf.clear(); } catch (IOException e) { e.printStackTrace(System.err); } } try { outputFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }