java写入代码 用java写代码

如何用java写这段代码?import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;public class JEncrytion{
public static void main(String[] argv) {
try{KeyGenerator keygenerator = KeyGenerator.getInstance("DES");SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;// Create the cipher
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Initialize the cipher for encryption
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);//sensitive information
byte[] text = "No body can see me".getBytes();
System.out.println("Text [Byte Format] : " + text);
System.out.println("Text : " + new String(text));
// Encrypt the text
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("Text Encryted : " + textEncrypted);
// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);// Decrypt the text
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("Text Decryted : " + new String(textDecrypted));
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(NoSuchPaddingException e){
e.printStackTrace();
}catch(InvalidKeyException e){
e.printStackTrace();
}catch(IllegalBlockSizeException e){
e.printStackTrace();
}catch(BadPaddingException e){
e.printStackTrace();
}
}
}
怎么写java代码?只要自己的电脑安装了jdk环境,任何地方都可以进行java代码的编写的,记事本也可以 。
跪求Java中写入文件和从文件中读取数据的最佳的代码!import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class IOTest {
public static void main(String[] args) {
String str = "123\r\n456";
writeFile(str);//写
String str1 = readFile();//读
System.out.println(str1);
}
/**
* 传递写java写入代码的内容
* @param str
*/
static void writeFile(String str) {
try {
File file = new File("d:\\file.txt");
if(file.exists()){//存在
file.delete();//删除再建
file.createNewFile();
}else{
file.createNewFile();//不存在直接创建
}
FileWriter fw = new FileWriter(file);//文件写IO
fw.write(str);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 返回读取的内容
* @return
*/
static String readFile() {
String str = "", temp = null;
try {
File file = new File("d:\\file.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);//文件读IO
while((temp = br.readLine())!=null){//读到结束为止
str += (temp+"\n");
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
刚写的java写入代码,够朋友好好学习一下啦java写入代码,呵呵
多多看APIjava写入代码,多多练习
如何在java里输入代码?。?/h2>首先要使用到从控制台输入
scanner
sc
=
new
scanner(system.in);
然后使用可变参数和累加,当然你需要有输入完成的标记 , 比如输入的为0时结束,求之前所有数的和
public
int
getsum(int...
num){
int
result
=
0;
for(int
n
:
a){
result
+=
n;
}
return
result;

推荐阅读