java在Eclipse中,如何将文本文件作为命令行参数读入?
我有以下代码:
import java.util.*;
import java.io.*;
public class LetterPattern{
// value for letter when there is no match
public static final char UNRECOGNIZED_LETTER = ' ';
// size of pattern array
public static final int PATTERN_SIZE = 10;
// size of grid array
public static final int GRID_SIZE = PATTERN_SIZE + 2;
/*
To hold the pattern, its features, and the letter
it represents. If the pattern does not match any
of the letter specifications, then the letter char
is a blank.
*/
int[][] grid;
int
massbottom,
corners,
tees;
char
letter;
// default constructor
// grid will be created as a 12x12 array of 0's
// the integer data members are initialized to 0
// by default already; letter is set to ' '
public LetterPattern()
{
grid = new int[GRID_SIZE][GRID_SIZE];
letter = UNRECOGNIZED_LETTER;
}
/*
precondition: assumes sc is not null and has a least one
more line of text in it.
postcondition: grid is loaded with 0's and 1's according to
the discussion in the project description based on what
remains in the input Scanner sc.
YOU HAVE TO CODE THIS.
*/
public void loadPattern(Scanner sc)
{
int r = 1; //row number
while(sc.hasNextLine())
{
//read in line
String gridFill = sc.nextLine();
for(int i = 0; i < GRID_SIZE; i++)
{
if(gridFill.charAt(i) == '*')
{
grid[r][i] = 1;
}
else if(gridFill.charAt(i) == '$')
{
//ignore rest of the line, fill it with 0s
for(int k = i; k < GRID_SIZE; k++)
{
grid[r][k] = 0;
}
}
else
{
grid[r][i] = 0;
}
}
r++;
if( r == 12)
{
break;
}
}
sc.close();
}
/*
precondition: assumes the grid array is not null and is a 12x12
array with 0's and 1's.
postcondition: the instance specific data members, massbottom,
corners, and tees are calculated from the current contents of
grid according to the project specification.
YOU HAVE TO CODE THIS.
*/
public void extractFeatures()
{
//read in row by row, looking for corners and tees, if it is last row, then do massbottom.
for(int row = 1; row < PATTERN_SIZE + 1; row++)
{
for(int column = 1; column < PATTERN_SIZE +1; column++)
{
int squareValue = grid[row][column];
if(row == PATTERN_SIZE)
{
//count 1s for the massbottom
if(squareValue == 1)
{
massbottom++;
}
}
if(squareValue == 1)
{ //look for corners/tees
//check north of vertex
int northSquare = grid[row-1][column];
//check south of vertex
int southSquare = grid[row+1][column];
//check east of vertex
int eastSquare = grid[row][column+1];
//check west of vertex
int westSquare = grid[row][column-1];
int sumSquares = westSquare + eastSquare + southSquare + northSquare;
//Counts the squares around the vertex, if there are only 2 "1's", then it is a corner, if three then a tee.
//any more or less is invalid.
if(sumSquares == 2)
{
//checks for valid corner
if(westSquare == 1 && northSquare == 1)
{
corners++;
break;
}
else if(westSquare == 1 && southSquare == 1)
{
corners++;
break;
}
else if(southSquare == 1 && eastSquare == 1)
{
corners++;
break;
}
else if(eastSquare ==1 && northSquare == 1)
{
corners++;
break;
}
}
else if(sumSquares == 3)
{
//checks for valid tee
if(westSquare == 1 && northSquare == 1 && southSquare == 1)
{
tees++;
break;
}
else if(westSquare == 1 && southSquare == 1 && eastSquare == 1)
{
tees++;
break;
}
else if(southSquare == 1 && eastSquare == 1 && northSquare == 1)
{
tees++;
break;
}
else if(eastSquare ==1 && northSquare == 1 && westSquare == 1)
{
tees++;
break;
}
}
}
}
}
}
/*
precondition: assumes the massbottom, corners, and tees
data members have been correctly calculated from the
current contents of the grid.
postcondition: letter is assigned either the UNRECOGNIZED_LETTER
value if the features do not match any of the letter features,
or the letter whose features are matched.
YOU HAVE TO CODE THIS.
*/
public void classifyLetter()
{
//massbottom of 1 possible letters are "F', "I", "P", "T", and "Y".
if(massbottom == 1)
{ //check for number of corners and tees to validate a letter
if(corners == 1 && tees == 1)
{
letter = 'F';
}
else if(corners == 0 && tees == 0)
{
letter = 'I';
}
else if(corners == 3 && tees == 1)
{
letter = 'P';
}
else if(corners == 0 && tees == 1)
{
letter = 'T';
}
else if(corners == 2 && tees == 1)
{
letter = 'Y';
}
}
//massbottom of 2 possible letters are "A", "H", and "M"
else if(massbottom == 2)
{ //check for number of corners and tees to validate a letter
if(corners == 2 && tees == 2)
{
letter = 'A';
}
else if(corners == 0 && tees == 2)
{
letter = 'H';
}
else if(corners == 2 && tees == 1)
{
letter = 'M';
}
}
//massbottom > 2 possible letters are "B", "C", "E", "G", "L", and "S".
else if(massbottom > 2)
{ //check for number of corners and tees to validate a letter
if(corners == 4 && tees == 2)
{
letter = 'B';
}
else if(corners == 2 && tees == 0)
{
letter = 'C';
}
else if(corners == 2 && tees == 1)
{
letter = 'E';
}
else if(corners == 3 && tees == 1)
{
letter = 'G';
}
else if(corners == 1 && tees == 0)
{
letter = 'L';
}
else if(corners == 4 && tees == 0)
{
letter = 'S';
}
}
else
{
letter = UNRECOGNIZED_LETTER;
}
}
// getter functions for the massbottom, tees, corners, and
// the matching letter
public int getMassbottom(){ return massbottom;}
public int getCorners(){ return corners;}
public int getTees(){ return tees;}
public char getLetter(){ return letter;}
/*
pre: grid is not null
post: grid is not modified its full contents(all 12 rows of 12 columns)
has been printed to the screen, line by line
YOU MUST CODE THIS.
*/
public void printPatternToScreen()
{
//print out the grid
for (int i = 0; i < GRID_SIZE; i++)
{
for (int j = 0; j < GRID_SIZE; j++)
{
System.out.print(" " + grid[i][j]);
}
System.out.println("");
}
}
/*
pre: grid, massbottom, corners, tees, and letter are all
consistently loaded;
patternNum indicates which number pattern this is from the
input file
post: the values of massbottom, corners, tees, and letter are
reported to standard out labeled with patternNum
*/
public void reportResultsToStdout(int patternNum){
System.out.println("\nResults for pattern# " + patternNum + "\n");
printPatternToScreen();
System.out.println("\n Massbottom = " + massbottom
+ "\n Num of Corners = " + corners +
"\n Num of Tees = " + tees);
System.out.print("\n These feature values ");
if (letter == LetterPattern.UNRECOGNIZED_LETTER)
System.out.println("do not match any letter.");
else
System.out.println("match " + letter);
}
/*
This main can be used to obtain your output.
It sets up a Scanner instance from a command line argument,
creates a LetterPattern instance, and repeatedly loads the
LetterPattern instance from the Scanner and
analyzes it. It displays the results to the standard out.
*/
public static void main(String[] args){
Scanner src;
LetterPattern lp = new LetterPattern();
int
patternNumber = 1;
if (args.length > 0){
try{
src = new Scanner(new File(args[0]));
while (src.hasNextLine()){
lp.loadPattern(src);
lp.extractFeatures();
lp.classifyLetter();
lp.reportResultsToStdout(patternNumber);
patternNumber++;
}
}
catch(NullPointerException e){
System.out.println("The file name may have been a null string.\nProgram Terminating." + e);
}
catch(FileNotFoundException e){
System.out.println("No file with the name " + args[0]
+ "\nProgram terminating." + e);
}
}
else // args is empty
System.out.println("You must supply the input file name on"
+ " the command line.");
}
}
我试图通过eclipse从命令行读取我的“letters.txt”文件。我做了一些研究,发现了如何通过运行配置输入命令行参数。现在,当我运行它时,我得到的只是这个消息
程序终止。JAVA木卫一。FileNotFoundException:字母。txt(系统找不到指定的文件)
我的文件位于src和bin所在的同一级别文件夹中。(字母|模式>;bin | src |类路径|字母)
此外,我确实将我的java文件复制到我的笔记本电脑上,并重新制作了一个项目,然后将java文件导入。这会影响到什么吗
# 1 楼答案
当使用完整路径文件名
"D:\path\to\file\letters.txt"
运行程序时,它似乎可以工作。如果你的文件夹名中有空格,不要忘记双引号