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);
    }
}
1

3 Answers 3

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());
            }
    }
}        
Sign up to request clarification or add additional context in comments.

Comments

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 !

1 Comment

yesss!! i copied your code and i changed the last part where aChar - c i changed on this: for(int i =0;i<res.lenght();i++) res[i]=chars2[i]-chars[i] check it out!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.