我今天早些时候在这里发布了一个类似的问题,我知道我不应该在这里提出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