List of usage examples for java.io File File
public File(URI uri)
From source file:Main.java
public static void main(final String[] args) throws IOException { File infile = new File("/tmp/utf16.txt"); FileInputStream inputStream = new FileInputStream(infile); Reader in = new InputStreamReader(inputStream, "UTF-16"); int read;//ww w. j av a2s. c o m while ((read = in.read()) != -1) { System.out.print(Character.toChars(read)); } in.close(); }
From source file:GuaranteeAFile.java
public static void main(String[] args) { String filename = "C:/myFile.txt"; File aFile = new File(filename); if (aFile.isDirectory()) { System.out.println("The path " + aFile.getPath() + " does not specify a file. Program aborted."); System.exit(1);/*from ww w. ja v a2 s . c o m*/ } if (!aFile.isFile()) { aFile = aFile.getAbsoluteFile(); File parentDir = new File(aFile.getParent()); if (!parentDir.exists()) { parentDir.mkdirs(); } } FileOutputStream outputFile = null; try { outputFile = new FileOutputStream(aFile, true); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } }
From source file:Main.java
public static void main(String[] args) { boolean areFilesIdentical = true; File file1 = new File("c:\\file1.txt"); File file2 = new File("c:\\file2.txt"); if (!file1.exists() || !file2.exists()) { System.out.println("One or both files do not exist"); System.out.println(false); }//from w w w . j a v a2 s . c o m System.out.println("length:" + file1.length()); if (file1.length() != file2.length()) { System.out.println("lengths not equal"); System.out.println(false); } try { FileInputStream fis1 = new FileInputStream(file1); FileInputStream fis2 = new FileInputStream(file2); int i1 = fis1.read(); int i2 = fis2.read(); while (i1 != -1) { if (i1 != i2) { areFilesIdentical = false; break; } i1 = fis1.read(); i2 = fis2.read(); } fis1.close(); fis2.close(); } catch (IOException e) { System.out.println("IO exception"); areFilesIdentical = false; } System.out.println(areFilesIdentical); }
From source file:Main.java
public static void main(String[] args) { File outputFile = new File("test.txt"); try (FileChannel fileChannel = new FileOutputStream(outputFile).getChannel()) { String text = getText();/*ww w .ja v a 2 s.co m*/ byte[] byteData = text.toString().getBytes("UTF-8"); ByteBuffer buffer = ByteBuffer.wrap(byteData); fileChannel.write(buffer); } catch (IOException e1) { e1.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { File inputFile = new File("test1.txt"); if (!inputFile.exists()) { System.out.println("The input file " + inputFile.getAbsolutePath() + " does not exist."); System.out.println("Aborted the file reading process."); return;/*w w w . j a va 2 s . co m*/ } try (FileChannel fileChannel = new FileInputStream(inputFile).getChannel()) { ByteBuffer buffer = ByteBuffer.allocate(1024); while (fileChannel.read(buffer) > 0) { buffer.flip(); while (buffer.hasRemaining()) { byte b = buffer.get(); System.out.print((char) b); } buffer.clear(); } } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { File file = new File("myimage.gif"); FileInputStream fis = new FileInputStream(file); Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//server.local:1521/prod", "scott", "tiger"); conn.setAutoCommit(false);//from w w w . ja va2 s . com PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)"); ps.setString(1, file.getName()); ps.setBinaryStream(2, fis, (int) file.length()); ps.executeUpdate(); ps.close(); fis.close(); }
From source file:Main.java
public static void main(String[] args) { boolean areFilesIdentical = true; File file1 = new File("c:\\file1.txt"); File file2 = new File("c:\\file2.txt"); if (!file1.exists() || !file2.exists()) { System.out.println("One or both files do not exist"); System.out.println(false); }//from w ww. ja v a 2 s .com System.out.println("length:" + file1.length()); if (file1.length() != file2.length()) { System.out.println("lengths not equal"); System.out.println(false); } try { FileInputStream fis1 = new FileInputStream(file1); FileInputStream fis2 = new FileInputStream(file2); int i1 = fis1.read(); int i2 = fis2.read(); while (i1 != -1) { if (i1 != i2) { areFilesIdentical = false; break; } i1 = fis1.read(); i2 = fis2.read(); } fis1.close(); fis2.close(); } catch (IOException e) { System.out.println("IO exception"); areFilesIdentical = false; } System.out.println(areFilesIdentical); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { File aFile = new File("C:/test.bin"); RandomAccessFile ioFile = new RandomAccessFile(aFile, " rw"); FileChannel ioChannel = ioFile.getChannel(); final int PRIMESREQUIRED = 10; long[] primes = new long[PRIMESREQUIRED]; int index = 0; final long REPLACEMENT = 999999L; final int PRIMECOUNT = (int) ioChannel.size() / 8; MappedByteBuffer buf = ioChannel.map(FileChannel.MapMode.READ_WRITE, 0L, ioChannel.size()).load(); ioChannel.close();//from w w w . java 2 s . com for (int i = 0; i < PRIMESREQUIRED; i++) { index = 8 * (int) (PRIMECOUNT * Math.random()); primes[i] = buf.getLong(index); buf.putLong(index, REPLACEMENT); } for (long prime : primes) { System.out.printf("%12d", prime); } ioFile.close(); }
From source file:Test.java
public static void main(String[] args) throws Exception { Path path = Paths.get(new URI("file:///C:/home/docs/users.txt")); File file = new File("C:\\home\\docs\\users.txt"); Path toPath = file.toPath();/* ww w . j av a 2s . c o m*/ System.out.println(toPath.equals(path)); }
From source file:Main.java
public static void main(String[] args) throws IOException { String fileName = "randomaccessfile.txt"; File fileObject = new File(fileName); if (!fileObject.exists()) { initialWrite(fileName);/*from w w w. j a v a2 s. c o m*/ } readFile(fileName); readFile(fileName); }