List of usage examples for java.io FileInputStream getChannel
public FileChannel getChannel()
From source file:MainClass.java
public static void main(String[] args) throws IOException { FileInputStream inFile = new FileInputStream(args[0]); FileOutputStream outFile = new FileOutputStream(args[1]); FileChannel inChannel = inFile.getChannel(); FileChannel outChannel = outFile.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inChannel.close();/* w w w . j a v a2s. c o m*/ outChannel.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { File fromFile = new File("fromFile.txt"); File toFile = new File("toFile.txt"); FileInputStream inFile = new FileInputStream(fromFile); FileOutputStream outFile = new FileOutputStream(toFile); FileChannel inChannel = inFile.getChannel(); FileChannel outChannel = outFile.getChannel(); int bytesWritten = 0; long byteCount = inChannel.size(); while (bytesWritten < byteCount) { bytesWritten += inChannel.transferTo(bytesWritten, byteCount - bytesWritten, outChannel); }/*from w ww . ja v a 2 s.c o m*/ inFile.close(); outFile.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { createFile();// w ww . jav a2 s. com File aFile = new File("C:/primes.bin"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); final int PRIMESREQUIRED = 10; ByteBuffer buf = ByteBuffer.allocate(8 * PRIMESREQUIRED); long[] primes = new long[PRIMESREQUIRED]; int index = 0; // Position for a prime in the file // Count of primes in the file final int PRIMECOUNT = (int) inChannel.size() / 8; // Read the number of random primes required for (int i = 0; i < PRIMESREQUIRED; i++) { index = 8 * (int) (PRIMECOUNT * Math.random()); inChannel.read(buf, index); // Read the value buf.flip(); primes[i] = buf.getLong(); // Save it in the array buf.clear(); } for (long prime : primes) { System.out.printf("%12d", prime); } inFile.close(); // Close the file and the channel }
From source file:Main.java
public static void main(String[] args) throws Exception { String source = "s.txt"; String destination = "d.txt"; FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(destination); FileChannel fci = fis.getChannel(); FileChannel fco = fos.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while (true) { int read = fci.read(buffer); if (read == -1) break; buffer.flip();// w w w . j a va 2s. c o m fco.write(buffer); buffer.clear(); } }
From source file:Main.java
static public void main(String args[]) throws Exception { FileInputStream fin = new FileInputStream("infile.txt"); FileOutputStream fout = new FileOutputStream("outfile.txt"); FileChannel inc = fin.getChannel(); FileChannel outc = fout.getChannel(); ByteBuffer bb = ByteBuffer.allocate(1024); while (true) { int ret = inc.read(bb); if (ret == -1) break; bb.flip();/*from w w w. j ava2 s . c o m*/ outc.write(bb); bb.clear(); } }
From source file:Main.java
static public void main(String args[]) throws Exception { FileInputStream fin = new FileInputStream("infile.txt"); FileOutputStream fout = new FileOutputStream("outfile.txt"); FileChannel inc = fin.getChannel(); FileChannel outc = fout.getChannel(); ByteBuffer bb = ByteBuffer.allocateDirect(1024); while (true) { int ret = inc.read(bb); if (ret == -1) break; bb.flip();//from ww w .j a v a 2 s. c om outc.write(bb); bb.clear(); } }
From source file:MainClass.java
public static void main(String[] args) throws IOException { FileInputStream inFile = new FileInputStream(args[0]); FileOutputStream outFile = new FileOutputStream(args[1]); FileChannel inChannel = inFile.getChannel(); FileChannel outChannel = outFile.getChannel(); for (ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); inChannel.read(buffer) != -1; buffer.clear()) { buffer.flip();/*from w w w . j a v a 2s. c o m*/ while (buffer.hasRemaining()) outChannel.write(buffer); } inChannel.close(); outChannel.close(); }
From source file:WordCount.java
public static void main(String args[]) throws Exception { String filename = "WordCount.java"; // Map File from filename to byte buffer FileInputStream input = new FileInputStream(filename); FileChannel channel = input.getChannel(); int fileLength = (int) channel.size(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength); // Convert to character buffer Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharBuffer charBuffer = decoder.decode(buffer); // Create line pattern Pattern linePattern = Pattern.compile(".*$", Pattern.MULTILINE); // Create word pattern Pattern wordBreakPattern = Pattern.compile("[\\p{Punct}\\s}]"); // Match line pattern to buffer Matcher lineMatcher = linePattern.matcher(charBuffer); Map map = new TreeMap(); Integer ONE = new Integer(1); // For each line while (lineMatcher.find()) { // Get line CharSequence line = lineMatcher.group(); // Get array of words on line String words[] = wordBreakPattern.split(line); // For each word for (int i = 0, n = words.length; i < n; i++) { if (words[i].length() > 0) { Integer frequency = (Integer) map.get(words[i]); if (frequency == null) { frequency = ONE;//w w w . j a va2 s.c o m } else { int value = frequency.intValue(); frequency = new Integer(value + 1); } map.put(words[i], frequency); } } } System.out.println(map); }
From source file:MainClass.java
public static void main(String[] args) throws IOException { FileInputStream inFile = new FileInputStream(args[0]); FileOutputStream outFile = new FileOutputStream(args[1]); FileChannel inChannel = inFile.getChannel(); FileChannel outChannel = outFile.getChannel(); FileLock outLock = outChannel.lock(); FileLock inLock = inChannel.lock(0, inChannel.size(), true); inChannel.transferTo(0, inChannel.size(), outChannel); outLock.release();/*from w w w. java 2s . c o m*/ inLock.release(); inChannel.close(); outChannel.close(); }
From source file:MainClass.java
public static void main(String[] args) throws IOException { FileInputStream inFile = new FileInputStream(args[0]); FileOutputStream outFile = new FileOutputStream(args[1]); FileChannel inChannel = inFile.getChannel(); FileChannel outChannel = outFile.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); int bytesRead = 0; while (bytesRead >= 0 || buffer.hasRemaining()) { if (bytesRead != -1) bytesRead = inChannel.read(buffer); buffer.flip();/*w w w . j a va 2s . c o m*/ outChannel.write(buffer); buffer.compact(); } inChannel.close(); outChannel.close(); }