在Java中跳过空白

时间:2019-03-22 01:10:19

标签: java whitespace filereader

所以我只是想办法完成我的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 个答案:

答案 0 :(得分: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();
        }
    }
}