我试图从input.txt
读取每一行并打印每一行,这样输入行中的每个字母如果是小写,则变为大写,如果是大写,则变为小写。
另外,我想使用Thread
来执行此操作,因为我也想打印每行的反面。
我收到了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());
}
}
答案 0 :(得分:0)
这是因为 嵌套 类printUppLow
和printRev
未声明为static
,因此与实例相关。
要解决此问题,请将它们从主类中声明,或声明在其声明中添加static
关键字。
(顺便说一句,你可能会抱怨你没有用大写字母命名你的课程。)
答案 1 :(得分:0)
这是使用嵌套的Classes for Runnable接口的示例。
package co.edu.unbosque.jn.in2outtext;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* @author Alejandro
*/
public class FileManager {
private File toRead;
BufferedReader br;
public FileManager(String fileIN) throws FileNotFoundException {
this.toRead = new File(fileIN);
if (!this.toRead.exists()) {
throw new IllegalArgumentException("File doesn't exist");
}
br = new BufferedReader(new FileReader(toRead));
}
public String reverseUpLowReadLine() throws IOException {
String line = br.readLine();
StringBuilder newSt = new StringBuilder();
if (line != null) {
for (Character c : line.toCharArray()) {
if (Character.isLowerCase(c)) {
newSt.append(Character.toUpperCase(c));
} else {
newSt.append(Character.toLowerCase(c));
}
}
} else {
br.close();
return null;
}
return newSt.toString();
}
public String reverseLine() throws IOException {
String line = br.readLine();
if (line != null) {
StringBuilder sb = new StringBuilder(line);
return sb.reverse().toString();
} else {
br.close();
return null;
}
}
}
=============================================== ==============
package co.edu.unbosque.jn.in2outtext;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Alejandro Leon
*/
public class In2Out {
public static void main(String[] args) {
String inFile = "In.txt";
try {
final FileManager fm = new FileManager(inFile);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
String s = null;
do {
try {
s = fm.reverseLine();
if (s != null) {
System.out.println(s);
}
} catch (IOException ex) {
Logger.getLogger(In2Out.class.getName()).log(Level.SEVERE, null, ex);
}
} while (s != null);
}
});
final FileManager fm2 = new FileManager(inFile);
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
String s = null;
do {
try {
s = fm2.reverseUpLowReadLine();
if (s != null) {
System.out.println(s);
}
} catch (IOException ex) {
Logger.getLogger(In2Out.class.getName()).log(Level.SEVERE, null, ex);
}
} while (s != null);
}
});
t.start();
t2.start();
} catch (FileNotFoundException ex) {
Logger.getLogger(In2Out.class.getName()).log(Level.SEVERE, null, ex);
}
}
}