简体   繁体   English

如何将txt文件转换为字符串

[英]How do I convert my txt file to a String

How to I convert my txt file to a String that I read in my newGame method? 如何将txt文件转换为在newGame方法中读取的String I need to use my given interface. 我需要使用给定的界面。 The txt file used is a 9x9 matrix. 使用的txt文件是9x9矩阵。 Then I need to convert that into a 2D array, how may I go about converting the String file in to a 2D int file as well. 然后,我需要将其转换为2D数组,如何将String文件也转换为2D int文件。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;


public class GameManager implements SudokuBoardManager
{

private static GameManager myBoard;

public static void main(String[] args)
{
    myBoard = new GameManager();
    JFileChooser chooser = new JFileChooser();
    myBoard.newGame(chooser.getSelectedFile());
    System.out.println(myBoard.toString());


}

@Override
public void setValueAt(int r, int c, int v) throws InputOutOfRangeException, ValueNotValidException 
{


}

@Override
public int getValueAt(int r, int c) throws InputOutOfRangeException 
{
    return 0;
}

@Override
public int[] displayPossibleValues(int r, int c)throws InputOutOfRangeException 
{
    return null;
}

public String toString()
{

    return " ";
}

@Override
public void newGame(File gameFile) 
{
    JFileChooser chooser = new JFileChooser();
    int status;
    Scanner in = null;

    chooser.setDialogTitle("Select Sudoku Game File");
    status = chooser.showOpenDialog(null);
    if(status == JFileChooser.APPROVE_OPTION)
    {
        try
        {
            gameFile = chooser.getSelectedFile();
            in = new Scanner(gameFile); 
        }
        catch(InputMismatchException e)
        {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}




}

尝试-

String output = new Scanner(new File("file.txt")).useDelimiter("\\Z").next();

You could match with your data with the interface using a comma/newline delimited Scanner with nested for loop: 您可以使用带有嵌套for循环的逗号/换行分隔的Scanner ,通过接口与数据进行匹配:

Scanner scanner = new Scanner(new File("game.txt")).useDelimiter(",|\r\n");
for (int i=0; i < 9; i++) {
   for (int j=0; j < 9; j++) {
      myBoard.setValueAt(i, j, scanner.nextInt());
   }
}

Setter would look like this: 塞特犬看起来像这样:

public void setValueAt(int r, int c, int v) throws InputOutOfRangeException, ValueNotValidException {
    // Handle InputOutOfRangeException, ValueNotValidException
    boardValues[r][c] = v;
}

Just read each line using BufferedReaders readline() method. 只需使用BufferedReaders readline()方法读取每一行。 Then find the integers in that line one by one & keep adding them in your 2d array. 然后逐行查找该行中的整数,并将其继续添加到2d数组中。

Have a look at org.apache.commons.io.IOUtils.readFully() and org.apache.commons.io.IOUtils.readLines . 看看org.apache.commons.io.IOUtils.readFully()org.apache.commons.io.IOUtils.readLines

The url is http://commons.apache.org/io/ 网址是http://commons.apache.org/io/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM