Edit Linux Network Configuration File("/etc/network/interfaces") through code - java

I want to add a functionality in the App where the user can change machine IP scheme (IP, SubnetMask, DefaultGateway) permanently, So I want to do Read/Write operation on the Linux Network Configuration File ("/etc/network/interfaces") using following code.
File file = new File("/etc/network/interfaces");
boolean exists = file.exists();
String line = "";
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
try
{
FileReader fr = new FileReader(file.getAbsoluteFile());
//BufferedReader br = new BufferedReader(fr);
Scanner scan = new Scanner(new FileInputStream(file));
if(exists)
{
while(scan.hasNext()) //while((line = br.readLine()) != null)
{
// Any Write operation
}
scan.close(); // br.close
}
}
bw.close();
Problem is that the check on while() loop keeps returning false.
I did some research for any alternative for that which includes using BufferedReader or Scanner to read the file but didn't work. All the following checks just keep returning false.
while(scan.hasNext())
while(scan.hasNextLine())
while((line = br.readLine()) != null)
Although file does exist, it contains its content But every time I try to read it with the above code all file content gets removed and the file gets empty.
Am I missing something? Is there any better alternative? I've also tried reading another file in the same directory which has full permission of read/write/execute for all users but still same result

As I'm trying to open the file to write and read was what causing the issue and loop gets terminated in the beginning. So it turns out that You should not use FileWriter before FileReader for same file. Doing so at the same time causing File reader to read empty file and loop terminates as it gets EndOfFile right at the beginning. Afterwards it closes the file empty hence all its contents are being lost.
Better way was to
First open the file for 'Read' only.
Scan through file line by line & keep a buffer of each line parsed (List in my case).
Add the content you wish to update in the file when you get to your Marker line & update you buffer as well.
Now open the file to 'Write' you updated list on it
Note: This is suitable if the file size is reasonably small to accommodate the file processing time.
File file = new File("/etc/network/interfaces");
boolean exists = file.exists();
Scanner scanner = null;
PrintWriter wirtter = null;
String line = "";
List<String> fileLines = new ArrayList<String>();
if(exists)
{
try {
scanner = new Scanner(new FileInputStream(file));
while(scanner.hasNextLine())
{
line = scanner.nextLine();
fileLines.add(line);
if(line.trim().startsWith("iface eth0 inet static"))
{
while(scanner.hasNextLine())
{
line = scanner.nextLine();
fileLines.add(line);
if(line.trim().startsWith("address"))
{
String updateStr = "\taddress "+ipAddress+"\t";
fileLines.remove(fileLines.size()-1);
fileLines.add(updateStr);
System.out.println("IP add updated");
}
else if(line.trim().startsWith("netmask"))
{
String updateStr = "\tnetmask "+subnetMask+"\t";
fileLines.remove(fileLines.size()-1);
fileLines.add(updateStr);
System.out.println("subnet add updated");
}
else if(line.trim().startsWith("gateway"))
{
String updateStr = "\tgateway "+defaultGateway+"\t";
fileLines.remove(fileLines.size()-1);
fileLines.add(updateStr);
System.out.println("Gatway add updated");
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
if(scanner != null)
scanner.close();
}
Now do the Writing Separately. And Also you'd want to restart the networking service
try {
wirtter = new PrintWriter(file);
for (String lineW : fileLines)
wirtter.println(lineW);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
if(wirtter != null)
wirtter.close();
}
}
synchronized (p) {
String cmd = "sudo /etc/init.d/networking restart ";
p.exec(cmd);
p.wait(10000);
System.out.println("finishing restart 'Networking:' service");
}

Related

Writing to File From Input Log

So I'm having a few troubles here. I need to be able to write my output to a file, and have it contain only the keywords specified in the code. This code is writing nothing to the file, and it only opens another box for user input. How do I get it to close the input box after the user inputs the file name, get it to write the output to the file, and get the output to display in the compiler? Thanks!
import java.util.*;
import java.io.*;
public class Classname {
static Scanner sc = new Scanner(System.in);
public static void main(String args[]) throws IOException,
FileNotFoundException {
String filename;
// Connecting to a file with a buffer
PrintWriter outFile = new PrintWriter(
new BufferedWriter(
new FileWriter("chatOutput.log")));
// Get the file
System.out.print("Please enter full name of the file: ");
filename = sc.next();
// Assign the name of the text file to a file object
File log = new File( filename);
String textLine = null; // Null
String outLine = ""; // Null
while(sc.hasNext())
{
String line=sc.nextLine();
if(line.contains("LANTALK"))
System.out.println(line);
}
try
{
// assigns the file to a filereader object..this will throw an error if
the file does not exist or cannot be found
BufferedReader infile = new BufferedReader(new FileReader(log));
try
{
// read data from a file..this will throw and error if something goes
wrong reading (empty or past end of file)
while((textLine = infile.readLine()) != null)
{
//System.out.printf("%s\n",textLine);
outLine = textLine.toUpperCase();
outFile.printf("%s\n",outLine);
}// end of while
} // end of try
finally // finally blocks get executed even if an exception is thrown
{
infile.close();
outFile.close();
}
}// end of try
catch (FileNotFoundException nf) // this goes with the first try because it
will throw a FileNotFound exception
{
System.out.println("The file \""+log+"\" was not found");
}
catch (IOException ioex) // this goes with the second try because it will
throw an IOexception
{
System.out.println("Error reading the file");
}
} /// end of main
} // end of class
What you need is to end the while(sc.hasNext()) while loop because the Scanner sc will always have a next because you are literally saying asking yourself if you got the line from the user then wait for next line with sc.nextLine(); then you are putting it into a string so next time you ask yourself do i have the line the answer is yes,anyways it's a little complicated to get over this issue you need to change the while loop to have a special word that will brake it,so you have to change it from:
while(sc.hasNext()){
String line=sc.nextLine();
if(line.contains("LANTALK"))
System.out.println(line);
}
To,for example:
while(true){
String line=sc.nextLine();
if(line.contains("LANTALK"))
System.out.println(line);
if(line.contains("END"))
break;
}
Also you need to check if the file entered by the user exists and actually add the text from the console to the file,so it would look something like this:
if(!log.exists())log.createNewFile();
// Connecting to a file with a buffer
PrintWriter logFile = new PrintWriter(
new BufferedWriter(
new FileWriter(log.getAbsolutePath())));
while(true){
String line=sc.nextLine();
if(line.contains("LANTALK"))
System.out.println(line);
if(line.contains("END"))
break;
logFile.println(line);
}
logFile.close();
Now all we have to do is print the output to the console when writing it to the logFile,so the while((textLine = infile.readLine()) != null),will now look a little something like this:
while((textLine = infile.readLine()) != null)
{
//System.out.printf("%s\n",textLine);
outLine = textLine.toUpperCase();
outFile.println(outLine);
System.out.println(outLine);
}// end of while
} // end of try
So in the end the hole thing should look a little something like this:
import java.util.*;
import java.io.*;
public class Classname{
static Scanner sc = new Scanner(System.in);
public static void main(String args[]) throws IOException,
FileNotFoundException {
String filename;
// Connecting to a file with a buffer
PrintWriter outFile = new PrintWriter(
new BufferedWriter(
new FileWriter("chatOutput.log")));
// Get the file
System.out.print("Please enter full name of the file: ");
filename = sc.next();
// Assign the name of the text file to a file object
File log = new File(filename);
String textLine = null; // Null
String outLine = ""; // Null
if(!log.exists())log.createNewFile();
// Connecting to a file with a buffer
PrintWriter logFile = new PrintWriter(
new BufferedWriter(
new FileWriter(log.getAbsolutePath())));
while(true){
String line=sc.nextLine();
if(line.contains("LANTALK"))
System.out.println(line);
if(line.contains("END"))
break;
logFile.println(line);
}
logFile.close();
try{
// assigns the file to a filereader object..this will throw an error if the file does not exist or cannot be found
BufferedReader infile = new BufferedReader(new FileReader(log));
try
{
// read data from a file..this will throw and error if something goes wrong reading (empty or past end of file)
while((textLine = infile.readLine()) != null)
{
//System.out.printf("%s\n",textLine);
outLine = textLine.toUpperCase();
outFile.println(outLine);
System.out.println(outLine);
}// end of while
} // end of try
finally // finally blocks get executed even if an exception is thrown
{
infile.close();
outFile.close();
}
}// end of try
catch (FileNotFoundException nf) // this goes with the first try because it will throw a FileNotFound exception
{
System.out.println("The file \""+log+"\" was not found");
}
catch (IOException ioex) // this goes with the second try because it will throw an IOexception
{
System.out.println("Error reading the file");
}
} /// end of main
} // end of class
If this is not what you are looking for i'm sorry,but i tried to make it do want you described you wanted,i mean it does write the output to the file, and get the output to display in the compiler,here's what the compiler console looks like:
Please enter full name of the file: test.txt
hi
hi
hi
END
HI
HI
HI
I'm sorry if this is not what you wanted but i tried my best,hope it helps.

How to rename a txt file using Java after copying contents from another txt file

I have created a program where there is a file called groups.txt. This file contains a list of names. To delete a group, it has to exist within the file. I used the Scanner method to search through each line for the name. If it contains the line, it sets val as 1. Which triggers the val == 1 condition. What I wanted to do during this block, is try to delete groupName from the groups.txt file. To do this, I created a new txt file called TempFile which copies all the names from groups.txt EXCEPT groupName. This file is then renamed to groups.txt and the old groups.txt file is deleted.
Everything works as intended, except the renaming. The temp.txt file still exists and the groups.txt file is unchanged. I checked the boolean success, and it always returns as false. Any ideas how to solve this?
if (method.equals("delete group")){
int val = 0;
String groupName = myClient.readLine();
try {
Scanner file = new Scanner(new File("groups.txt"));
while (file.hasNextLine()){
String line = file.nextLine();
if (line.indexOf(groupName) != -1){
val = 1;
}
}
if (val == 1){
try {
File groupFile = new File("groups.txt");
File tempFile = new File("temp.txt");
BufferedReader reader = new BufferedReader(new FileReader(groupFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String currentLine;
System.out.println(groupName);
while ((currentLine = reader.readLine()) != null){
String trimLine = currentLine.trim();
if (trimLine.equals(groupName)){
continue;
} else {
writer.write(currentLine + System.getProperty("line.separator"));
}
}
writer.close();
reader.close();
groupFile.delete();
boolean success = tempFile.renameTo("groups.txt");
} catch (IOException f){
System.err.println("File Not Found: " + f.getMessage());
} }
} catch (FileNotFoundException f){
System.err.println("File Not Found Exception: " + f.getMessage());
}
}
CODE BEFORE THE ABOVE:
if (command.equals("group")){
String method = myClient.readLine();
if (method.equals("create group")){
String groupName = myClient.readLine();
int val = 0;
try {
Scanner file = new Scanner(new File("groups.txt"));
while (file.hasNextLine()){
String line = file.nextLine();
if (line.indexOf(groupName) != -1){
Report.error("group name already exists, please pick another");
val = 1;
}
}
} catch (FileNotFoundException f){
System.err.println("File Not Found: " + f.getMessage());
}
if (val == 0){
try {
PrintWriter out = new PrintWriter(new FileWriter("groups.txt", true));
out.println(groupName);
out.close();
} catch (IOException e){
Report.error("IOException: " + e.getMessage());
}
}
In the second part of the code, this is where I originally update the groups.txt file. So every time the user adds a group, it updates the groups.txt file by adding the new groupName to the end of the file. First, I make sure the groupName doesn't already exist using Scanner. myClient is a BufferedReader which reads from another class which stores what the user types in the command line.
Also do not forget to close Scanner. First you should make delete() work and make sure you know your current working directory, and write your filepath relative to it. Check with:
File file = new File("abc.txt");
System.out.println(file.getAbsolutePath());
One thing might be unrelated, also check your environment because
In the Unix'esque O/S's you cannot renameTo() across file systems. This behavior is different than the Unix "mv" command. When crossing file systems mv does a copy and delete which is what you'll have to do if this is the case. The same thing would happen on Windows if you tried to renameTo a different drive, i.e. C: -> D:

set encoding as UTF-8 for a FileWriter [duplicate]

This question already has answers here:
Write a file in UTF-8 using FileWriter (Java)?
(9 answers)
Closed 5 years ago.
Below is my code, it is intended to take two .ckl files, compare the two, add the new items and create a new merged file. The program executes correctly when run in Netbeans however, when executing the .jar the program doesn't appear to be encoding the file in UTF-8. I am rather new to programming and would like to know where or how I might need to be enforcing this encoding to take place?
** I have removed the Swing code and other lines so that only my method is shown, the method that does all of the comparing and merging.
public void mergeFiles(File[] files, File mergedFile) {
ArrayList<String> list = new ArrayList<String>();
FileWriter fstream = null;
BufferedWriter out = null;
try {
fstream = new FileWriter(mergedFile, false);
out = new BufferedWriter(fstream);
} catch (IOException e1) {
e1.printStackTrace();
}
// Going in a different direction. We are using a couple booleans to tell us when we want to copy or not. So at the beginning since we start
// with our source file we set copy to true, we want to copy everything and insert vuln names into our list as we go. After that first file
// we set the boolean to false so that we dont start copying anything from the second file until it is a vuln. We set to true when we see vuln
// and set it to false if we already have that in our list.
// We have a tmpCopy to store away the value of copy when we see a vuln, and reset it to that value when we see an </VULN>
Boolean copy = true;
Boolean tmpCopy = true;
for (File f : files) {
textArea1.append("merging files into: " + mergedFilePathway + "\n");
FileInputStream fis;
try {
fis = new FileInputStream(f);
// BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(mergedFile), "UTF-8"));
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String aLine;
while ((aLine = in.readLine()) != null) {
// Skip the close checklist and we can write it in at the end
if (aLine.trim().equals("</iSTIG>")) {
continue;
}
if (aLine.trim().equals("</STIGS>")) {
continue;
}
if (aLine.trim().equals("</CHECKLIST>")) {
continue;
}
if (aLine.trim().equals("<VULN>")) {
// Store our current value of copy
tmpCopy = copy;
copy = true;
String aLine2 = in.readLine();
String aLine3 = in.readLine();
String nameLine = in.readLine();
if (list.contains(nameLine.trim())) {
textArea1.append("Skipping: " + nameLine + "\n");
copy = false;
while (!(aLine.trim().equals("</VULN>"))) {
aLine = in.readLine();
}
continue; // this would skip the writing out to file part
} else {
list.add(nameLine.trim());
textArea1.append("::: List is now :::");
textArea1.append(list.toString() + "\n");
}
if (copy) {
out.write(aLine);
out.newLine();
out.write(aLine2);
out.newLine();
out.write(aLine3);
out.newLine();
out.write(nameLine);
out.newLine();
}
} else if (copy) {
out.write(aLine);
out.newLine();
}
// after we have written to file, if the line was a close vuln, switch copy back to original value
if (aLine.trim().equals("</VULN>")) {
copy = tmpCopy;
}
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
copy = false;
}
// Now lets add the close checklist tag we omitted before
try {
out.write("</iSTIG>");
out.write("</STIGS>");
out.write("</CHECKLIST>");
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Java has extensive, highly informative documentation. Keep it bookmarked. Refer to it first, whenever you have difficulty. You'll find it's frequently helpful.
In this case, the documentation for FileWriter says:
The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.
If you want to be sure your file will be written as UTF-8, replace this:
FileWriter fstream = null;
BufferedWriter out = null;
try {
fstream = new FileWriter(mergedFile, false);
with this:
Writer fstream = null;
BufferedWriter out = null;
try {
fstream = new OutputStreamWriter(new FileOutputStream(mergedFile), StandardCharsets.UTF_8);
For those, who use FileWriter in order to append to an existing file, the following will work
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8)) {
//code
}
You can just run it with the command java -Dfile.encoding=UTF-8 -jar yourjar.jar.
Follow this for more info.

i want to change the text in a file, my code is searching the word but not replacing the word

I am trying to replace a string from a js file which have content like this
........
minimumSupportedVersion: '1.1.0',
........
now 'm trying to replace the 1.1.0 with 1.1.1. My code is searching the text but not replacing. Can anyone help me with this. Thanks in advance.
public class replacestring {
public static void main(String[] args)throws Exception
{
try{
FileReader fr = new FileReader("G:/backup/default0/default.js");
BufferedReader br = new BufferedReader(fr);
String line;
while((line=br.readLine()) != null) {
if(line.contains("1.1.0"))
{
System.out.println("searched");
line.replace("1.1.0","1.1.1");
System.out.println("String replaced");
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
First, make sure you are assigning the result of the replace to something, otherwise it's lost, remember, String is immutable, it can't be changed...
line = line.replace("1.1.0","1.1.1");
Second, you will need to write the changes back to some file. I'd recommend that you create a temporary file, to which you can write each `line and when finished, delete the original file and rename the temporary file back into its place
Something like...
File original = new File("G:/backup/default0/default.js");
File tmp = new File("G:/backup/default0/tmpdefault.js");
boolean replace = false;
try (FileReader fr = new FileReader(original);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(tmp);
BufferedWriter bw = new BufferedWriter(fw)) {
String line = null;
while ((line = br.readLine()) != null) {
if (line.contains("1.1.0")) {
System.out.println("searched");
line = line.replace("1.1.0", "1.1.1");
bw.write(line);
bw.newLine();
System.out.println("String replaced");
}
}
replace = true;
} catch (Exception e) {
e.printStackTrace();
}
// Doing this here because I want the files to be closed!
if (replace) {
if (original.delete()) {
if (tmp.renameTo(original)) {
System.out.println("File was updated successfully");
} else {
System.err.println("Failed to rename " + tmp + " to " + original);
}
} else {
System.err.println("Failed to delete " + original);
}
}
for example.
You may also like to take a look at The try-with-resources Statement and make sure you are managing your resources properly
If you're working with Java 7 or above, use the new File I/O API (aka NIO) as
// Get the file path
Path jsFile = Paths.get("C:\\Users\\UserName\\Desktop\\file.js");
// Read all the contents
byte[] content = Files.readAllBytes(jsFile);
// Create a buffer
StringBuilder buffer = new StringBuilder(
new String(content, StandardCharsets.UTF_8)
);
// Search for version code
int pos = buffer.indexOf("1.1.0");
if (pos != -1) {
// Replace if found
buffer.replace(pos, pos + 5, "1.1.1");
// Overwrite with new contents
Files.write(jsFile,
buffer.toString().getBytes(StandardCharsets.UTF_8),
StandardOpenOption.TRUNCATE_EXISTING);
}
I'm assuming your script file size doesn't cross into MBs; use buffered I/O classes otherwise.

Directory listing not refreshing

This is driving me crazy! I have a panel that displays a list of files from a directory. The list is stored in a vector. When I click on a button, a new file is created in the same directory and the list needs to be refreshed.
I don't understand why Java cannot see the new file, even though if I add a good old DIR in Dos, Dos can see the file. It's as if the new file is invisible even though I can see it and Dos can see it. I tried giving it some time (sleep, yield) but it's no use. I also tried copying to a new temp file and reading the temp but again to no avail. Here's some code (removed some irrelevant lines):
public class Button extends EncapsulatedButton {
public Button()
{
super("button pressed");
}
public void actionPerformed(ActionEvent arg0) {
//removed function here where the new file is created in the directory
//remove call to DOS that regenerates /myFileList.txt after a new file was added in the directory
//at this point, DOS can see the new file and myFileList.txt is updated, however it is read by java without the update!!!!!
//now trying to read the directory after the new file was created
Vector data = new Vector<String>();
String s = null;
// Create the readers to read the file.
try {
File f = new File("/myFileList.txt");
BufferedReader stream = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
while((s = stream.readLine()) != null)
{
data.addElement(s.trim());
}
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
util();
}
void util(){
//giving it time is not helping
Thread.yield();
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
//get the file listing through java instead of DOS - still invisible
File fLocation = new File("/myDir");
File[] filesFound = fLocation.listFiles();
for (int i = 0; i < filesFound.length; i++) {
if (filesFound[i].isFile()) {
System.out.println("**********" + filesFound[i].getName());
}
}
//last resort: copy to a temp then read from there - still not good
try{
//copy to a temp file
File inputFile = new File("/myFileList.txt");
File outputFile = new File("/myFileList_temp.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
//read the copy to see if it is updated
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("/myFileList_temp.txt");
// Get the object of DataInputStream
DataInputStream in1 = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in1));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println ("Test file read: " + strLine);
}
//Close the input stream
in1.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
I would appreciate any leads. Thank you.
myFileList.txt lokks like this:
myline1
myline2
myline3
After adding a new file in the folder,
myline4 should appear in it, then it will be read and displayed in the panel.
Honestly, your code is a mess.
You read from /myFileList.txt and do nothing with what you read, except store it in a temporary Vector. At best, this has no effect; at worst (if the file doesn't exist, for example) it throws an exception. Whatever it does, it does not create a new file.
Furthermore, I see no reference to the panel in your GUI that supposedly displays the file list. How do you expect it to get updated?
This works for me:
To refresh the directory list, call .listFiles() again.
filesFound = fLocation.listFiles();
should show the most updated directory listing. Hope this helps you.

Categories

Resources