我的代码存在问题。我有一个println语句,在netbeans中打印一个字符串到输出部分(控制台)。但是当我在命令提示符下编译并运行它时,程序不会打印输出。我将System.out.println(plaintext)
替换为System.out.println("Hello world")
并且它有效。代码如下。
netbeans输出:
gjfjdjddkslskaskdkmfaoadfadvg
和控制台:
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:\Users\KJ4CC>cd C:\Users\KJ4CC\OneDrive\Documents\NetBeansProjects\cipher\src\cipher
C:\Users\KJ4CC\OneDrive\Documents\NetBeansProjects\cipher\src\cipher>javac Cipher.java
C:\Users\KJ4CC\OneDrive\Documents\NetBeansProjects\cipher\src\cipher>cd ..
C:\Users\KJ4CC\OneDrive\Documents\NetBeansProjects\cipher\src>java cipher.Cipher
C:\Users\KJ4CC\OneDrive\Documents\NetBeansProjects\cipher>
这是代码:
package cipher;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
*
* @author KJ4CC
*/
public class Cipher {
char[] text;
public static void main(String[] args) {
String plainText;
String cipherText;
Cipher cipher = new Cipher();
//these arnt used as of right now. will pass this to the encrypt method, for the text to be encrypted.
String original = "";
String iv = "fsf";
String key = "hel";
//calls read file, that will put the text that needs to be encrypted into a char array.
plainText = cipher.readFile(iv);
cipherText = cipher.encrypt(original, iv, key);
System.out.printf(plainText);
cipher.output(plainText,cipherText,iv,key);
}
public String encrypt(String orig, String iv, String key) {
int i, j, result;
String cipherText = "";
int b = iv.length();
//loops through the entire set of chars
/*for (i = 0; i < text.length; i += b) {
//Splits the char into block the size of the IV block.
for (j = 0; j < b; j++) {
if(i == 0){
result = ((iv.charAt(j) - 'a') + (text[j + i] - 'a'))%26;
}else{
result = ((key.charAt(j) - 'a') + (text[j + i] - 'a'))%26;
}
}
// System.out.println(", ");
}
*/
return cipherText;
}
public String readFile(String iv) {
String plainText = "";
try {
//opens file and reads all of the text into a string.
String fileString = new Scanner(new File("text.txt"))
.useDelimiter("\\A+").next();
//removes all of the spaces and special chars, to be put into the char array
fileString = fileString.replaceAll(" ", "");
fileString = fileString.replaceAll("[',',':','.',' ','(', ' )', '--','/','\\s']", "");
//puts the editied string back into a char array for storage.
//adding letters too bufer the iv
int bufferLetters = fileString.length();
bufferLetters = fileString.length() % iv.length();
if (bufferLetters > 0) {
bufferLetters = iv.length() - bufferLetters;
for (int i = 0; i < bufferLetters; i++) {
fileString = fileString + "x";
}
}
fileString = fileString.toLowerCase();
plainText = fileString;
text = fileString.toCharArray();
} catch (IOException e) {
}
return plainText;
}
public void output(String plainText, String cipherText, String iv, String key){
}
}
java版:
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
答案 0 :(得分:2)
text.txt可能位于NetBeans的文件路径上,但是当您从命令提示符编译/执行javac / java时则不行。在后一种情况下,您会收到IOException,因为找不到该文件,但catch块为空。尝试将一个System.out.println()添加到catch块中,并在使用独立的javac / java编译/执行时打印该消息