I have an issue about parsing args. I have a write method which is in WriteToFile
class like this:
我有一个关于解析args的问题。我有一个WriteToFile类的write方法,如下所示:
public class WriteToFile {
public void write (String text){
try{
FileWriter writer = new FileWriter("output.txt",true);
BufferedWriter write = new BufferedWriter(writer);
write.write(text);
write.newLine();
write.close();
}
catch (Exception error){
error.printStackTrace();
}
}
public class A {
WriteToFile write = new WriteToFile();
write.write(something);
}
public class B {
WriteToFile write = new WriteToFile();
write.write(something);
}
We need to change "output.txt" to args[1]. Normally adding one more argument to method is the solution but allowed only 3 line code changes. I tried to add a set method but write method used by multiple classes so it takes more than 3 lines to fix it. I tried to extend main class to get into scope, didn't work. Is there any way to do it?
我们需要将“output.txt”更改为args [1]。通常向方法添加一个参数是解决方案,但只允许3行代码更改。我尝试添加一个set方法但是写了多个类使用的方法,所以修复它需要3行以上。我试图扩展主类进入范围,没有工作。有什么办法吗?
3 个解决方案
#1
1
1) create new public field in the WriteToFile
class (fileName)
1)在WriteToFile类(fileName)中创建新的公共字段
2) from main myWriter.fileName = args[1]
2)来自main myWriter.fileName = args [1]
3) FileWriter writer = new FileWriter(fileName, true);
3)FileWriter writer = new FileWriter(fileName,true);
// no change to the method signature called 100 times
//没有更改方法签名调用100次
Edit
If you are constructing many instances of WriteToFile all over the place, then use a static field in Main
如果要在整个地方构建WriteToFile的许多实例,那么在Main中使用静态字段
1) In Main
public static String fileName;
1)在Main public static String fileName中;
2) from main Main.fileName = args[1]
2)来自main Main.fileName = args [1]
3) FileWriter writer = new FileWriter(Main.fileName, true);
3)FileWriter writer = new FileWriter(Main.fileName,true);
#2
4
Adding a parameter and using it when you call the method would only take three changes:
在调用方法时添加参数并使用它只需要进行三处更改:
// Change 1 - add a fileName parameter to the method
public void write (String text, String fileName) {
try{
FileWriter writer = new FileWriter(fileName, true); // Change 2 - use it
BufferedWriter write = new BufferedWriter(writer);
write.write(text);
write.newLine();
write.close();
}
catch (Exception error){
error.printStackTrace();
}
}
// Change 3 - in the main method that calls this one, pass args[1]:
myWriter.write("someText", args[1]);
#3
1
public void write (String text, String filename) // 1
{
FileWriter writer = new FileWriter(filename,true); // 2
...
In your main, do this while calling.
在你的主要部分,在打电话时这样做。
new WriteToFile().write(text, args[1]); // 3
These are exactly 3 line changes as shown by comments.
这些正好是3行更改,如注释所示。
#1
1
1) create new public field in the WriteToFile
class (fileName)
1)在WriteToFile类(fileName)中创建新的公共字段
2) from main myWriter.fileName = args[1]
2)来自main myWriter.fileName = args [1]
3) FileWriter writer = new FileWriter(fileName, true);
3)FileWriter writer = new FileWriter(fileName,true);
// no change to the method signature called 100 times
//没有更改方法签名调用100次
Edit
If you are constructing many instances of WriteToFile all over the place, then use a static field in Main
如果要在整个地方构建WriteToFile的许多实例,那么在Main中使用静态字段
1) In Main
public static String fileName;
1)在Main public static String fileName中;
2) from main Main.fileName = args[1]
2)来自main Main.fileName = args [1]
3) FileWriter writer = new FileWriter(Main.fileName, true);
3)FileWriter writer = new FileWriter(Main.fileName,true);
#2
4
Adding a parameter and using it when you call the method would only take three changes:
在调用方法时添加参数并使用它只需要进行三处更改:
// Change 1 - add a fileName parameter to the method
public void write (String text, String fileName) {
try{
FileWriter writer = new FileWriter(fileName, true); // Change 2 - use it
BufferedWriter write = new BufferedWriter(writer);
write.write(text);
write.newLine();
write.close();
}
catch (Exception error){
error.printStackTrace();
}
}
// Change 3 - in the main method that calls this one, pass args[1]:
myWriter.write("someText", args[1]);
#3
1
public void write (String text, String filename) // 1
{
FileWriter writer = new FileWriter(filename,true); // 2
...
In your main, do this while calling.
在你的主要部分,在打电话时这样做。
new WriteToFile().write(text, args[1]); // 3
These are exactly 3 line changes as shown by comments.
这些正好是3行更改,如注释所示。