在Java中输入文本文件时遇到问题

时间:2015-03-20 00:29:46

标签: java file text inputstream insertion

我的教授真的把我们带进了这个项目。我们没有深入研究如何在Java中使用和插入文件。我收到了大量错误,这很可能是因为我错误地插入了文件。我将文本文件保存在我的计算机上保存类文件的相同位置,假设这是必要的。我已经将它移动到计算机上的多个位置,试图让它工作。这是主程序。如果它完全不正确,我很抱歉。

为了解释我们应该做些什么,这里是带有伪代码的提示的链接。我还没有尝试执行列出的所有操作,因为我还没有正确插入文件。 http://jcsites.juniata.edu/faculty/rhodes/cs1/projects/program9Gen.html

编辑:这是整个计划的荣耀。该类是在一个单独的项目中创建的,作为我们对Java类的介绍。我们被告知要再次使用它并在底部插入主程序以便于评分。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class GenSeq
{
   private String seq;
   private String species;
   private String startCodon;
   private String stopCodon;
   private String shape;
   private String chromosomeLocation;

   public GenSeq (String seq, String species, String startCodon, String stopCodon,
   String shape, String chromosomeLocation){
     this.seq = seq;
     this.species = species;
     this.startCodon = startCodon;
     this.stopCodon = stopCodon;
     this.shape = shape;
     this.chromosomeLocation = chromosomeLocation;
   }
   //Allowing the program to later set constructors

   //Creating all the appropriate getters and setters for the instance variables
   public void setSpecies(String newSpecies){
      species = newSpecies;
   }

   public String getSpecies(){
      return species;
   }

   public void setStartCodon(String newStartCodon){
      startCodon = newStartCodon;
   }

   public String getStartCodon(){
      return startCodon;
   }

   public void setStopCodon(String newStopCodon){
      stopCodon = newStopCodon;
   }

   public String getStopCodon(){
      return stopCodon;
   }

   public void setShape(String newShape){
      shape = newShape;
   }

   public String getShape(){
      return shape;
   }

   public void setChromosomeLocation(String newChromosomeLocation){
      chromosomeLocation = newChromosomeLocation;
   }

   public String getChromosomeLocation(){
      return chromosomeLocation;
   }

   public String toString(){
     return "Sequence length: " + seq.length() +
                       "\nSpecies: "+ species +
                       "\nStart Codon: "+ startCodon +
                       "\nStart Codon: "+ stopCodon+
                       "\nShape: "+ shape +
                       "\nChromosomal Location: " + chromosomeLocation;
     //Creating a toString method to hold all the class data
   }
}



public static void main (String args[ ])
{
  GenSeq seqA = null;
  //Setting constructor to default if not set
  //Opening the file
  Scanner inputStream = null;
  String seq;

  try
  {
    inputStream = new Scanner (new File ("W:\jcsites.junata.edu\students\morrian14\seq.txt"));
  }
  catch (FileNotFoundException e)
  {
    System.out.println ("Error opening the file ");
    System.exit (0);
  }
  do{
    inputStream = inputStream.trim();
    if ('>' == inputStream.charAt(0)){
      seq = inputStream.nextLine();
    }
  }
  while (inputStream.hasNextLine());
  inputStream.close();


}

错误是连续重复的错误

文件:C:\ LEXIPC \ Users \ Alexis \ GenSeq.java [line:83] 错误:预期的类,接口或枚举

2 个答案:

答案 0 :(得分:0)

一个明显的问题,最后一行显然是写成inputStream.close();而不是input.Stream.close();你可能需要try .. catch ...左右关闭流

答案 1 :(得分:0)

你的问题究竟是什么?虽然有几点说明...... 摆脱do{} while()并做这样的事情:

while(inputStream.hasNextLine(){
    if('>' == inputStream.charAt(0))
        seq = inputStream.nextLine();
}
inputStream.close();

我有点困惑为什么要回收seq来从文件中读取,因为这就是您用作文件名的内容。更好的方法是使用File类作为文件名。考虑一下:File seq = new File(.../filename.txt)。 此外,如果您发现使用了太多的try / catch块,请考虑使用异常处理类来清理代码。