有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中跳过空白

所以我只是想办法完成我的java项目,我很难跳过空白。除了这个,我几乎所有的方法都能用。如果有人能帮助我理解如何让FileInputStream跳过空白,那么我终于可以完成这个项目了。就在这一部分。这个类的目的(不仅仅是处理方法然后清理它)是接收一个文件(ASCII文本艺术)并通过一个完美的正方形“放大”。谢谢!

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class makeBiggerTesting {
    public static void main(String[] args) {
        String fileName = "";
        Scanner keyBoard = new Scanner(System.in);
        System.out.print("Enter fileName: ");
        fileName = keyBoard.nextLine();

        try {
            int zoom = 9;
            int i = (int) Math.sqrt((double) zoom);
            //Scanner fileScan = new Scanner(fileName);
            FileInputStream inputStream = new FileInputStream(fileName);
//          while(inputStream.available() > 0) {
//              char input = (char) inputStream.read();
//              System.out.print((char) input);
//              
//              }
//          System.out.println("");
            while(inputStream.available() > 0) {
            char input = (char) inputStream.read();

            for(int row = 1; row <= i; row++) {

                for(int col = 1; col <= i; col++) {
                    System.out.print((char) input);
                }
                System.out.println("");
            }
            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

以下是其中一个文件的外观:

@ @

output:
@@@ @@@
@@@ @@@
@@@ @@@

这就是它的本来面目。但我明白

@@@ (Bunch of space)@@@
@@@                 @@@
@@@                 @@@

当我运行程序时,它会变成一团乱麻,所以我认为它也会放大空白。但我可能错了


共 (1) 个答案

  1. # 1 楼答案

    我不确定我是否完全理解你的问题。你是在追求类似的东西吗

    public class makeBigger {
        public static void main(String[] args) {
            Scanner keyBoard = new Scanner(System.in);
            System.out.print("Enter fileName: ");
            String fileName = keyBoard.nextLine();
            int zoom = 9;
            int i = (int) Math.sqrt((double) zoom);         
            try(FileInputStream inputStream = new FileInputStream(fileName)){
                int input = -1;
                String line = "";
                while ((input = inputStream.read()) > 0 ) { 
                    if((char)input == ' ') {
                        line += (char) input;
                    }else if(input == 10 || input == 13) {                      
                        for (int col = 1; col <= i; col++) {                                
                            System.out.println(line);                           
                        }   
                        line = "";
                    }else {
                        line += new String(new char[i]).replace("\0", ""+(char)input);
                    }                       
                }
                if(! "".equals(line)) {
                    for (int col = 1; col <= i; col++) {                                
                        System.out.println(line);                           
                    }   
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {               
                e.printStackTrace();
            }
        }
    }