The following code is to create a new text file and save some text in it and then give user the choice either to open the file at the same time or not. (File will be opened using MS Word from its default directory)
Code:-
import java.io.*;
import java.nio.file.*;
import java.util.*;
public class Files {
static Process Microsoft;
Path path;
@SuppressWarnings("ResultOfObjectAllocationIgnored")
public void file(String pth) throws IOException {
path = Paths.get(pth);
if (!path.toFile().exists()) {
new java.io.File(path.toString()).createNewFile();
writeData(path.toString());
} else {
System.out.println("Destination File Already Exists!");
}
}
public void writeData(String pth) {
try {
Scanner scan = new Scanner(System.in);
Path path;
path = Paths.get(pth);
DataOutputStream out = new DataOutputStream(new FileOutputStream(path.toFile()));
String input, temp;
List<String> list = new ArrayList<>();
int i = 0;
char yn;
System.out.println("Enter Data: ");
while (scan.hasNextLine()) {
temp = scan.nextLine();
if (temp.matches("EXIT")) {
System.out.printf("Want to end Y/N ? ");
yn = new java.util.Scanner(System.in).next().charAt(0);
if (yn == 'Y' || yn == 'y') {
temp = "\0";
break;
} else {
System.out.println("You can continue with your work!");
temp = scan.nextLine().trim();
list.add(temp.trim());
input = list.get(i);
byte b[] = input.trim().getBytes();
out.write(b);
i++;
continue;
}
} else {
list.add(temp.trim());
input = list.get(i);
byte b[] = input.trim().getBytes();
out.write(b);
out.writeChar('\n');
i++;
}
}
String lst[] = new String[list.toArray().length];
System.out.printf("Open File Y/N ? ");
yn = new java.util.Scanner(System.in).next().charAt(0);
if (yn == 'Y' || yn == 'y') {
Microsoft = Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE " + path.toString());
Microsoft.waitFor();
} else if (yn == 'N' || yn == 'n') {
for (i = 0; i < lst.length; i++) {
System.out.println((lst[i] = list.get(i)));
}
}
} catch (IOException | InterruptedException e) {
System.err.println("Error Occur!\n" + e.getMessage());
System.exit(0);
}
}
public static void main(String[] args) throws IOException {
Files fls = new Files();
System.out.printf("Path: ");
String path = new java.util.Scanner(System.in).nextLine();
fls.file(path);
Microsoft.destroy();
}
}
Code:-
import java.io.*;
import java.nio.file.*;
import java.util.*;
public class Files {
static Process Microsoft;
Path path;
@SuppressWarnings("ResultOfObjectAllocationIgnored")
public void file(String pth) throws IOException {
path = Paths.get(pth);
if (!path.toFile().exists()) {
new java.io.File(path.toString()).createNewFile();
writeData(path.toString());
} else {
System.out.println("Destination File Already Exists!");
}
}
public void writeData(String pth) {
try {
Scanner scan = new Scanner(System.in);
Path path;
path = Paths.get(pth);
DataOutputStream out = new DataOutputStream(new FileOutputStream(path.toFile()));
String input, temp;
List<String> list = new ArrayList<>();
int i = 0;
char yn;
System.out.println("Enter Data: ");
while (scan.hasNextLine()) {
temp = scan.nextLine();
if (temp.matches("EXIT")) {
System.out.printf("Want to end Y/N ? ");
yn = new java.util.Scanner(System.in).next().charAt(0);
if (yn == 'Y' || yn == 'y') {
temp = "\0";
break;
} else {
System.out.println("You can continue with your work!");
temp = scan.nextLine().trim();
list.add(temp.trim());
input = list.get(i);
byte b[] = input.trim().getBytes();
out.write(b);
i++;
continue;
}
} else {
list.add(temp.trim());
input = list.get(i);
byte b[] = input.trim().getBytes();
out.write(b);
out.writeChar('\n');
i++;
}
}
String lst[] = new String[list.toArray().length];
System.out.printf("Open File Y/N ? ");
yn = new java.util.Scanner(System.in).next().charAt(0);
if (yn == 'Y' || yn == 'y') {
Microsoft = Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE " + path.toString());
Microsoft.waitFor();
} else if (yn == 'N' || yn == 'n') {
for (i = 0; i < lst.length; i++) {
System.out.println((lst[i] = list.get(i)));
}
}
} catch (IOException | InterruptedException e) {
System.err.println("Error Occur!\n" + e.getMessage());
System.exit(0);
}
}
public static void main(String[] args) throws IOException {
Files fls = new Files();
System.out.printf("Path: ");
String path = new java.util.Scanner(System.in).nextLine();
fls.file(path);
Microsoft.destroy();
}
}
Comments