我今天早些時候在這裏發佈了一個類似的問題,我知道我不應該在這裏提出HW問題。我的問題是作業,但它已完成並提交(和分級),我只是在這裏希望讓我的程序運行,更好地瞭解:)Java:將文件內容讀取並輸出爲字節格式
爲了表明我已經完成它,而不是嘗試瞞過之一,這裏有一個鏈接到提交頁面:http://i959.photobucket.com/albums/ae76/GoWxGaiA/SOHW2pic_zps25eb2d2a.jpg
的說明就在這裏:http://i959.photobucket.com/albums/ae76/GoWxGaiA/pic3_zpsbb6c6541.png
這裏是我到目前爲止並提交:
package Homework1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class HW1 {
public static void main(String[] args){
System.out.println("Enter a file name: c:/users/logan/desktop/untitled.png");
System.out.println("Byte# Byte\n0: 255\n1: 216\n2: 255\n3: 244\n4: 0\n5: 16\n6: 74\n7: 70\n8: 73");
File file = new File("C://Users//Logan//Desktop//Untitled.png");
try
{
FileInputStream fin = new FileInputStream(file);
byte fileContent[] = new byte[(int)file.length()];
fin.read(fileContent);
String strFileContent = new String(fileContent);
System.out.println("File content : ");
System.out.println(strFileContent);
}
catch(FileNotFoundException e) {
System.out.println("File not found" + e); }
catch(IOException ioe) {
System.out.println("Exception while reading the file " + ioe); }
}
}
所以我的問題是:
問題1:如何提示用戶輸入文件名而不是硬編碼?
問題2:如何使用循環顯示每個單獨的字節值?
所有和任何幫助是非常重視!
你可以在手上的文件名作爲參數'args'這會給你一些你需要的靈活性,如果參數爲null拋出一個異常。 byte [] byteArr = myString.getBytes(); for(byte b:byteArr) { // sys out b } –
好的,讓我試試吧。 – user3521436