java 编写 求大神帮助~~

simle微笑 2014-07-21 07:13:16
题目:
接受一个参数,参数表示本地一个文件的地址,
需要读取此地址的文件,并且通过16进制格式输出(每行有16个字符,并且每个字符中间有一个空格)
...全文
265 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
simle微笑 2014-07-22
  • 打赏
  • 举报
回复
引用 3 楼 czq355696960 的回复:
你写这个做什么的 ,直接用UE工具
我这是刚刚进公司,我们经理布置我的任务呢
student-ai 2014-07-22
  • 打赏
  • 举报
回复
引用 1 楼 wnf2009 的回复:
睡前大致写了下,后面的格式化处理自己写下吧,今天累死了,睡觉去了

	public static void outputFileWitHex(String path){
		byte[] buffer = null;
		FileInputStream fis = null;
		int n;
		String ret = "";
		File file = new File(path);
		try {
			fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
			for (int i = 0; i < buffer.length; i++) {
				String hex = Integer.toHexString(buffer[i] & 0xFF);
				if (hex.length() == 1) {
					hex = "0x" + hex;
				}
				ret += hex.toUpperCase();
			}
			System.out.println(ret);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
简单修改了下上述代码,功能已经可以实现,

public static void outputFileWitHex(String path){
        byte[] buffer = null;
        FileInputStream fis = null;
        int n;
        String ret = "";
        File file = new File(path);
        try {
            fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
            String lineSeparator = (String) java.security.AccessController.doPrivileged(
                    new sun.security.action.GetPropertyAction("line.separator"));
            for (int i = 0; i < buffer.length; i++) {
                String hex = Integer.toHexString(buffer[i] & 0xFF)+" ";//每个字符后面添加空格.
                if(i==15)
                {
                    //hex+=lineSeparator;//兼容所有系统
                        /*
                          换行符提示:
                          windows:\r\n
                          linux:\n
                        */

                        hex+="\r\n";//换行符,由于i从0开始,所以15个就换行.
                }
                if (hex.length() == 1) {
                    hex = "0x" + hex;
                }
                ret += hex.toUpperCase();
            }
            System.out.println(ret);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
czq355696960 2014-07-22
  • 打赏
  • 举报
回复
你写这个做什么的 ,直接用UE工具
simle微笑 2014-07-22
  • 打赏
  • 举报
回复
这位帅哥哥,能不能再麻烦你一下,把格式化加上把?我太笨了,弄了一上午都写不出来阿~~谢谢你了,
wnf2009 2014-07-21
  • 打赏
  • 举报
回复
睡前大致写了下,后面的格式化处理自己写下吧,今天累死了,睡觉去了

	public static void outputFileWitHex(String path){
		byte[] buffer = null;
		FileInputStream fis = null;
		int n;
		String ret = "";
		File file = new File(path);
		try {
			fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
			for (int i = 0; i < buffer.length; i++) {
				String hex = Integer.toHexString(buffer[i] & 0xFF);
				if (hex.length() == 1) {
					hex = "0x" + hex;
				}
				ret += hex.toUpperCase();
			}
			System.out.println(ret);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧