Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用线程从输入到输出打印大小写和反转_Java_Multithreading_Text Manipulation - Fatal编程技术网

Java 使用线程从输入到输出打印大小写和反转

Java 使用线程从输入到输出打印大小写和反转,java,multithreading,text-manipulation,Java,Multithreading,Text Manipulation,我试图读取input.txt中的每一行并打印每一行,这样输入行中的每一个字母如果是小写就变成大写,如果是大写就变成小写。 此外,我想用一个线程来完成这项工作,因为我还想打印每行的背面 我得到了一个printuplow uppLow=new printuplow的错误;和printRev=新的printRev 代码 这是因为嵌套类printuplow和printRev不是声明为静态的,因此是相对实例的 要解决这个问题,请在主类之外声明它们,或者在声明中声明并添加static关键字 顺便说一下,您可

我试图读取input.txt中的每一行并打印每一行,这样输入行中的每一个字母如果是小写就变成大写,如果是大写就变成小写。 此外,我想用一个线程来完成这项工作,因为我还想打印每行的背面

我得到了一个printuplow uppLow=new printuplow的错误;和printRev=新的printRev

代码

这是因为嵌套类printuplow和printRev不是声明为静态的,因此是相对实例的

要解决这个问题,请在主类之外声明它们,或者在声明中声明并添加static关键字

顺便说一下,您可能会收到投诉,说您没有用大写字母命名类。

这是一个使用嵌套类作为可运行接口的示例

=============================================================

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);
        }
    }
}

此问题已在先前的修订版中得到回答。在那之后请不要改变问题。如果您遇到新问题,请在链接/引用此帖子时将其作为新问题发布。
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());
    }
}
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);
        }
    }
}