我正在尝试从中读取每一行input.txt
并打印每一行,以便输入行中的每个字母如果是小写则变为大写,如果是大写则变为小写。此外,我想使用 aThread
来执行此操作,因为我还想打印每行的反面。
printUppLow uppLow = new printUppLow();
我得到一个错误printRev rev = new printRev();
non-static variable this cannot be referenced from a static context.
代码
public static void main(String args[])
{
String inputfileName="input.txt"; // A file with some text in it
String outputfileName="output.txt"; // File created by this program
String oneLine;
try {
// Open the input file
FileReader fr = new FileReader(inputfileName);
BufferedReader br = new BufferedReader(fr);
// Create the output file
FileWriter fw = new FileWriter(outputfileName);
BufferedWriter bw = new BufferedWriter(fw);
printRev rev = new printRev();
printUppLow uppLow = new printUppLow();
rev.start();
uppLow.start();
// Read the first line
oneLine = br.readLine();
while (oneLine != null) { // Until the line is not empty (will be when you reach End of file)
// Print characters from input file
System.out.println(oneLine);
bw.newLine();
// Read next line
oneLine = br.readLine();
}
// Close the streams
br.close();
bw.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
public class printUppLow extends Thread
{
public void run(String str)
{
String result = "";
for (char c : str.toCharArray())
{
if (Character.isUpperCase(c)){
result += Character.toLowerCase(c); // Convert uppercase to lowercase
}
else{
result += Character.toUpperCase(c); // Convert lowercase to uppercase
}
}
return result; // Return the result
}
}
public class printRev extends Thread
{
public void run()
{
StringBuffer a = new StringBuffer("input.txt");
System.out.println(a.reverse());
}
}