2016-04-28 17 views
0

Ich versuche, diese Aufgabe zu tun.Wie erstellt man eine Binärdatei

(Sum all the integers in a binary data file) 
* create a binary data file named Exercise17_03.dat 
* has been created and its data are created using writeInt(int) 
* in DataOutputStream. The file contains an unspecified number 
* of integers. Write a program to find the sum of the integers. 

mein Code:

package loan; 

import java.io.*; 


public class Exercise17_03 { 

    public static void main(String[] args) throws IOException { 

     // Get the file for this exercise 
     File file = new File("src/text files/Exercise17_03.dat"); 

     // if file doesn't exist create the file and write a random number of integers 
     if (!file.exists() || true) { 
      try (DataOutputStream out = 
         new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) { 

       int random = (int) (Math.random() * 200); 

       for (int i = 0; i < random; i++) { 
        out.writeInt((int)(Math.random() * 200)); 
       } 
      } 
     } 

     // Read the file and display the sum 
     try (DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) { 

      int sum = 0; 
      int count = input.available()/4; 
      System.out.println(count); 
      while (count > 0) { 
       sum += input.readInt(); 
       count--; 
      } 
      System.out.println("The sum is " + sum); 
     } 


    } 
} 

aus irgendeinem Grund ich diesen Fehler erhalten

Exception in thread "main" java.io.FileNotFoundException: src\text files\Exercise17_03.dat (The system cannot find the path specified) 
    at java.io.FileOutputStream.open0(Native Method) 
    at java.io.FileOutputStream.open(Unknown Source) 
    at java.io.FileOutputStream.<init>(Unknown Source) 
    at java.io.FileOutputStream.<init>(Unknown Source) 
    at loan.Exercise17_03.main(Exercise17_03.java:24) 

ich nicht ganz verstehen, was die Syntax mir sagt. Kann jemand mir helfen zu verstehen, damit ich in Zukunft nicht auf das Problem stoße. Auch in diesem Erstellen einer Binärdatei. Es sieht nicht gut für mich aus, danke.

Ich bin mir nicht sicher, warum die Frage als ein Duplikat markiert ist. Es ist anders. Dies ist eine Frage zu meiner individuellen Aufgabe und was mit meinem Code nicht stimmt. Ich sehe nicht, wie es ein Duplikat ist.

+1

Überprüfen Sie das Verzeichnis 'src/Textdateien /' existiert. – saka1029

+0

Welcher Teil von 'java.io.FileNotFoundException: src \ Textdateien \ Exercise17_03.dat (Das System kann den angegebenen Pfad nicht finden)' ist nicht selbsterklärend? –

+0

Siehe auch: [Wie erstellt man eine Datei und schreibt sie in eine Datei in Java?] (Http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a- Datei-in-Java? Rq = 1) –

Antwort

-1

Der Fehler gemeldet wird, ist in dieser Zeile:

File file = new File("src/text files/Exercise17_03.dat"); 

Sie brauchen etwas zu geben, bevor „src“, um anzuzeigen, wo die Datei/Ordner sein soll.

-1

Ich würde vermuten, dass entweder das src Verzeichnis oder dessen text files Unterverzeichnis nicht relativ zum aktuellen Verzeichnis existiert die Laufzeit von ausgeführt wird, da die FileNotFoundException vom FileOutputStream Konstruktor geworfen wird.

Verwandte Themen