0

My code doesn't work correctly, I'm trying to decrypt a message but instead I get something like , 0, 3, ,, , 5, 7, <, ;, , ;, 9, ,, (, 4, , , -, ,, ), (, , �, ￸]

Please help me find where am I am wrong:

public class WorkInFile {
    public static void main(String[] args) throws IOException {
        FileInputStream encoded=new FileInputStream("C://Games//encoded.txt");//contains ƪÄÖØÐîÃÜÙäÌÊÛÓÕÒáÄßÕÍǨ³¾êÉàÝâÝãƒâÝäìÚÇäÖçÅáâÄÄÌØÐƭèÑØǑÚÚŲã¨
            FileInputStream coded = new FileInputStream("C://Games//code.txt");//contains icbakwtbxxvcelsmjpbochqlltowxhlhvhyywsyqraargpdsycikmgeakonpiwcqmofwms
            String text = encoded.toString();
            String text2=coded.toString();
            char[] chars=text.toCharArray();
            char[] chars2=text2.toCharArray();

            int index=0;
            char[] res=new char[text.length()];
            for (char aChar : chars) {
                for (char c : chars2) {
                    res[index] = (char) (aChar - c);
                }
                index++;
            }
            String result= Arrays.toString(res);
            System.out.println(result);
    }
}
GameDroids
  • 5,584
  • 6
  • 40
  • 59
Eban Nick
  • 3
  • 1
  • Does this answer your question? [How do I create a Java string from the contents of a file?](https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file) – jhamon Jan 14 '20 at 08:28

3 Answers3

1

Files.readAllBytes(Paths.get("file-path"))

Java now offers a beautiful one-liner for reading file content.

Here is the working code for fetching file content as a string:

// WorkInFile.java

import java.nio.file.*;

public class WorkInFile {
    public static void main(String[] args) {
        try {
            String text = new String(Files.readAllBytes(Paths.get("encoded.txt")));
            System.out.println("Encoded.txt = " + text);

            String text2 = new String(Files.readAllBytes(Paths.get("code.txt")));
            System.out.println("code.txt = " + text2);

            } catch (Exception ex) {
                System.out.println(ex.toString());
            }
    }
}        
Gopinath
  • 4,066
  • 1
  • 14
  • 16
0

I think your problem lies out here:

String text = encoded.toString();
String text2=coded.toString();

You may refer to documentation to reach out that:

public String toString() Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode()) Returns: a string representation of the object.

So, toString() returns the representation of FileInputStream not the content of the stream.

Mohammed Deifallah
  • 1,290
  • 1
  • 10
  • 25
0

If the expected message is in Japanese and it talks about Soviet data, this code is for you.

You must use a BufferedReader for read the file and a StringBuilder for build a String with what the BufferedReader extracts from the file.

public static void main(String args[]) {

    String text;
    String text2;

    try {
        Path encodedPath = Paths.get("C://Games//encoded.txt");
        File encodedFile = new File(String.valueOf(encodedPath));

        Path codedPath = Paths.get("C://Games//code.txt");
        File codedFile = new File(String.valueOf(codedPath));

        StringBuilder codedBuilder = new StringBuilder();
        StringBuilder encodedBuilder = new StringBuilder();

        try (
            FileInputStream encoded = new FileInputStream(encodedFile.getAbsolutePath());
            FileInputStream coded = new FileInputStream(codedFile.getAbsolutePath())
        ) {

            try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(coded))) {
                String line;
                while ((line = bufferedReader.readLine()) != null){
                    codedBuilder.append(line);        
                }
                text = codedBuilder.toString();
            }
            try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(encoded))){
                String line;
                while ((line = bufferedReader.readLine()) != null){
                    encodedBuilder.append(line);
                }
                text2 = encodedBuilder.toString();
            }

            char[] chars = text.toCharArray();
            char[] chars2 = text2.toCharArray();

            int index = 0;
            char[] res = new char[text.length()];
            for (char aChar : chars) {
                for (char c : chars2) {
                    res[index] = (char) (aChar - c);
                }
                index++;
            }
            String result = Arrays.toString(res);
            System.out.println(result);
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

Let me know if that's what you wanted !

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
DLx
  • 97
  • 1
  • 13
  • 1
    yesss!! i copied your code and i changed the last part where aChar - c i changed on this: for(int i =0;i – Eban Nick Jan 14 '20 at 10:10