File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed
src/main/java/com/itersdesktop/javatechs/io Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .itersdesktop .javatechs .io ;
2+
3+ import java .io .File ;
4+ import java .io .IOException ;
5+ import java .nio .file .Files ;
6+ import java .nio .file .Path ;
7+ import java .nio .file .Paths ;
8+
9+ public class FileCreation {
10+ public static void main (String [] args ) throws IOException {
11+ String dir = "target" + File .separator + "indexing" ;
12+ Path dirPath = Paths .get (dir );
13+ if (!Files .exists (dirPath )) {
14+ Files .createDirectory (dirPath );
15+ }
16+
17+ String fileName = "test.txt" ;
18+ Path filePath = dirPath .resolve (fileName );
19+ if (!Files .exists (filePath )) {
20+ Files .createFile (filePath );
21+ }
22+
23+ File test = new File (dir , "test.txt" );
24+ if (test .exists ()) {
25+ System .out .println ("File has been created." );
26+ writeUsingFiles ("Dr Tung Nguyen" , test .getAbsolutePath ());
27+ } else {
28+ System .out .println ("Errors occurred when creating the file." );
29+ }
30+ }
31+
32+ private static void writeUsingFiles (String data , String absFilePath ) {
33+ try {
34+ Files .write (Paths .get (absFilePath ), data .getBytes ());
35+ } catch (IOException e ) {
36+ e .printStackTrace ();
37+ }
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package com .itersdesktop .javatechs .io ;
2+
3+ import java .nio .file .*;
4+
5+ public class JavaFileOpTest {
6+ public static void main (String [] args ) {
7+ System .out .println ("Testing methods of Java File Operations." );
8+ testPathResolve ();
9+ }
10+
11+ private static void testPathResolve () {
12+ // create an object of Path
13+ Path path = Paths .get ("/tmp/JavaOP" );
14+
15+ // create a string object
16+ String passedPath = "drive" ;
17+
18+ // call resolve() to create resolved Path
19+ Path resolvedPath = path .resolve (passedPath );
20+
21+ // print result
22+ System .out .println ("Resolved Path: " + resolvedPath );
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments