Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Dumping a file

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Dumping a file

    OK, I am creating a dump that prints out into a new file in hexadecimal format. I have written some code, but I am getting the wrong output. In the ActionListener (dump) button I am having trouble connecting a formatDump method which takes an int array and an int address as arguments. An then there is another method called hex2Str that has two arguments, one int that converts to hexadecimal and an int that determines the number of digits. How am I to connect both of these methods to print out into a new file using a button?
    import java.awt.Color;
     
     
    public class Week03 {
     
    	private JFrame frame;
    	private final JLabel lblNewLabel = new JLabel("@ ! @ Hex Dump @ ! @");
    	private final JLabel lblInstructions = new JLabel("Hex Dump program that " +
    			"will read a file and save the data that has been converted to " +
    			"hexadecimal form in a new file.");
    	private final JLabel lblFind = new JLabel("Find the following file:");
    	private final JTextField txtFind = new JTextField();
    	private final JButton btnFind = new JButton("Find");
    	private final JLabel lblSave = new JLabel("Save the following file:");
    	private final JTextField txtSave = new JTextField();
    	private final JButton btnSave = new JButton("Save");
    	private final JButton btnDump = new JButton("DUMP");
    	private static final int[] HEXARR = [hexStr];
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Week03 window = new Week03();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the application.
    	 */
    	public Week03() {
    		initialize();
    	}
     
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		txtFind.setFont(new Font("Andy", Font.PLAIN, 14));
    		txtFind.setBounds(10, 107, 589, 32);
    		txtFind.setColumns(10);
    		frame = new JFrame();
    		frame.setTitle("Belinda Frederick - Week03");
    		frame.setBackground(new Color(70, 130, 180));
    		frame.setBounds(100, 100, 715, 303);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
    		lblNewLabel.setForeground(new Color(46, 139, 87));
    		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
    		lblNewLabel.setFont(new Font("Fine Hand", Font.BOLD, 30));
    		lblNewLabel.setBounds(10, 11, 679, 50);
     
    		** bunch of code here **
     
    		frame.getContentPane().add(txtFind);
    		btnFind.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				JFileChooser jfc = new JFileChooser();
    				int inFile = jfc.showOpenDialog(null);
    				if(inFile == JFileChooser.APPROVE_OPTION) {
    					txtFind.setText(
    							jfc.getSelectedFile().getAbsolutePath());
    				}
    				txtSave.setText(txtFind.getText() + ".dmp");
    			}
    		});
    		btnFind.setForeground(new Color(248, 248, 255));
    		btnFind.setBackground(new Color(139, 0, 0));
    		btnFind.setFont(new Font("Batang", Font.BOLD, 14));
    		btnFind.setBounds(610, 109, 79, 30);
     
    		txtSave.setFont(new Font("Andy", Font.PLAIN, 14));
    		txtSave.setColumns(10);
    		txtSave.setBounds(10, 175, 589, 32);
     
    		frame.getContentPane().add(txtSave);
    		btnSave.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JFileChooser jfc = new JFileChooser();
    				int inFile = jfc.showSaveDialog(null);
    				if(inFile == JFileChooser.APPROVE_OPTION) {
    					txtSave.setText(
    							jfc.getSelectedFile().getAbsolutePath());
    				}
    			}
    		});
    		btnSave.setForeground(new Color(248, 248, 255));
    		btnSave.setFont(new Font("Batang", Font.BOLD, 14));
    		btnSave.setBackground(new Color(139, 0, 0));
    		btnSave.setBounds(610, 177, 79, 30);
     
    		frame.getContentPane().add(btnSave);
    		btnDump.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String inStr = txtFind.getText();
    				String outStr = txtSave.getText();
    				//String hexData = null;
    				FileInputStream fis = null;
    				PrintWriter pw = null;
    				try {
    					fis = new FileInputStream(inStr);
    					pw = new PrintWriter(outStr);
     
    					int k;
    					byte[] buffer = new byte[16];
    					while((k = fis.read(buffer)) != -1) {
    						for(int i = 0; i < k; i++) {
    							int j = buffer[i];
    							if(j < 0) j += 256;
    							pw.print(HEXARR);
    						}
    						pw.printf("\n");
    					}
    				} catch (Exception fe) {
     
    				} finally {
    					if(fis != null) {
    						try {
    							fis.close();
    						} catch (IOException e_a) {
    							e_a.printStackTrace();
    						}
    					} if(pw != null) {
    						pw.close();
    					}
    				}
    				JOptionPane.showMessageDialog(null, "The hex dump is " +
    						"complete.");
    			}
    		});	}
     
    	public static String formatDump(int address, int[] hexArr) {
    		String hexStr = "";
    		address = hexArr.length;
    		for(int i = 0; i < address; i ++) {
    			if((hexArr[i] > 32) && (hexArr[i] < 127) || (hexArr[i] > 160)) {
    				hexStr += hexArr + "  ";
    			} else {
    				hexStr += "\n";
    			}
    		}
    		return hexStr;
     
    		/**char[] hexChar = "0123456789ABCDEF".toCharArray();
    		address = 
    				8
    				+ 3;
    		int firstHex = address;
    		int fd = (address - 8)/4;
     
    		for(int i = fd; i < address; i++) {
    			if((hexArr[i] > 32) && (hexArr[i] < 127) || (hexArr[i] > 160)) {
    				hexStr += hexArr[i];
    			} else {
    				hexStr += ".";
    			}
    		}
    		int i = 0;
    		String hexStr = "";
    		StringBuffer sb = new StringBuffer(hexStr);
    		do {
    			for(int j = 0; j < hexArr.length; j++) {
     
    				i += address;
    			}
    		} while (i < hexArr.length);
    		int[] hexArr2 = new int[i];
    		return sb.toString();*/
    	}
     
    	public String hex2Str(int convert, int numDigit) {
    		StringBuilder strBldr = new StringBuilder(Integer.toString(convert, 16));
    		if(strBldr.length() < numDigit) {
    			int hex = (numDigit - strBldr.length());
    			for(int i = 0; i <= hex; i++) {
    				strBldr.insert(0, "0");
    			}
    		} else if(strBldr.length() > numDigit) {
    			strBldr = new StringBuilder();
    			for(int i = 1; i <= numDigit; i++) {
    				strBldr.append(".");
    			}
    		}
    		/**
    		//String str = String.format("%08x", Integer.valueOf(numDigit));
    		//String hexStr = "";
    		int num = 0;
    		for(int i = convert; i < convert + numDigit; i++) {
    			num = (convert >> 4 * (numDigit - i - 1)) & 0x0f;
    			//str = Integer.toString(num + 0x100, 16).substring(i);
    		}
    		return (HEXARR[num]);*/
    		return strBldr.toString();
     
     
     
    	}
    }

    Here is what I am outputting:

    nullnullnullnullnullnullnullnullnullnullnullnullnull


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dumping a file

    Your posted code does not compile. Either fix the compiler errors or post the full text of the error messages you need help fixing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dumping a file

    Here is the complete code:
    import java.awt.Color;
     
     
    public class Week03 {
     
    	private JFrame frame;
    	private final JLabel lblNewLabel = new JLabel("@ ! @ Hex Dump @ ! @");
    	private final JLabel lblInstructions = new JLabel("Hex Dump program that " +
    			"will read a file and save the data that has been converted to " +
    			"hexadecimal form in a new file.");
    	private final JLabel lblFind = new JLabel("Find the following file:");
    	private final JTextField txtFind = new JTextField();
    	private final JButton btnFind = new JButton("Find");
    	private final JLabel lblSave = new JLabel("Save the following file:");
    	private final JTextField txtSave = new JTextField();
    	private final JButton btnSave = new JButton("Save");
    	private final JButton btnDump = new JButton("DUMP");
    	private static final int[] HEXARR = null;;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Week03 window = new Week03();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the application.
    	 */
    	public Week03() {
    		initialize();
    	}
     
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		txtFind.setFont(new Font("Andy", Font.PLAIN, 14));
    		txtFind.setBounds(10, 107, 589, 32);
    		txtFind.setColumns(10);
    		frame = new JFrame();
    		frame.setTitle("Belinda Frederick - Week03");
    		frame.setBackground(new Color(70, 130, 180));
    		frame.setBounds(100, 100, 715, 303);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
    		lblNewLabel.setForeground(new Color(46, 139, 87));
    		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
    		lblNewLabel.setFont(new Font("Fine Hand", Font.BOLD, 30));
    		lblNewLabel.setBounds(10, 11, 679, 50);
     
    		frame.getContentPane().add(lblNewLabel);
    		lblInstructions.setForeground(new Color(139, 0, 0));
    		lblInstructions.setHorizontalAlignment(SwingConstants.CENTER);
    		lblInstructions.setFont(new Font("Corbel", Font.BOLD, 13));
    		lblInstructions.setBounds(10, 60, 679, 25);
     
    		frame.getContentPane().add(lblInstructions);
    		lblFind.setForeground(new Color(46, 139, 87));
    		lblFind.setFont(new Font("Harrington", Font.BOLD, 14));
    		lblFind.setBounds(10, 82, 227, 25);
     
    		frame.getContentPane().add(lblFind);
     
    		frame.getContentPane().add(txtFind);
    		btnFind.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				JFileChooser jfc = new JFileChooser();
    				int inFile = jfc.showOpenDialog(null);
    				if(inFile == JFileChooser.APPROVE_OPTION) {
    					txtFind.setText(
    							jfc.getSelectedFile().getAbsolutePath());
    				}
    				txtSave.setText(txtFind.getText() + ".dmp");
    			}
    		});
    		btnFind.setForeground(new Color(248, 248, 255));
    		btnFind.setBackground(new Color(139, 0, 0));
    		btnFind.setFont(new Font("Batang", Font.BOLD, 14));
    		btnFind.setBounds(610, 109, 79, 30);
     
    		frame.getContentPane().add(btnFind);
    		lblSave.setForeground(new Color(46, 139, 87));
    		lblSave.setFont(new Font("Harrington", Font.BOLD, 14));
    		lblSave.setBounds(10, 151, 227, 25);
     
    		frame.getContentPane().add(lblSave);
    		txtSave.setFont(new Font("Andy", Font.PLAIN, 14));
    		txtSave.setColumns(10);
    		txtSave.setBounds(10, 175, 589, 32);
     
    		frame.getContentPane().add(txtSave);
    		btnSave.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JFileChooser jfc = new JFileChooser();
    				int inFile = jfc.showSaveDialog(null);
    				if(inFile == JFileChooser.APPROVE_OPTION) {
    					txtSave.setText(
    							jfc.getSelectedFile().getAbsolutePath());
    				}
    			}
    		});
    		btnSave.setForeground(new Color(248, 248, 255));
    		btnSave.setFont(new Font("Batang", Font.BOLD, 14));
    		btnSave.setBackground(new Color(139, 0, 0));
    		btnSave.setBounds(610, 177, 79, 30);
     
    		frame.getContentPane().add(btnSave);
    		btnDump.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String inStr = txtFind.getText();
    				String outStr = txtSave.getText();
    				//String hexData = null;
    				FileInputStream fis = null;
    				PrintWriter pw = null;
    				try {
    					fis = new FileInputStream(inStr);
    					pw = new PrintWriter(outStr);
     
    					int k;
    					byte[] buffer = new byte[16];
    					while((k = fis.read(buffer)) != -1) {
    						for(int i = 0; i < k; i++) {
    							int j = buffer[i];
    							if(j < 0) j += 256;
    							pw.print(HEXARR);
    						}
    						pw.printf("\n");
    					}
    				} catch (Exception fe) {
     
    				} finally {
    					if(fis != null) {
    						try {
    							fis.close();
    						} catch (IOException e_a) {
    							e_a.printStackTrace();
    						}
    					} if(pw != null) {
    						pw.close();
    					}
    				}
    				JOptionPane.showMessageDialog(null, "The hex dump is " +
    						"complete.");
    			}
    		});
    		btnDump.setForeground(new Color(248, 248, 255));
    		btnDump.setBackground(new Color(46, 139, 87));
    		btnDump.setFont(new Font("Fine Hand", Font.BOLD, 18));
    		btnDump.setBounds(280, 222, 133, 32);
     
    		frame.getContentPane().add(btnDump);
    	}
     
    	public static String formatDump(int address, int[] hexArr) {
    		String hexStr = "";
    		address = hexArr.length;
    		for(int i = 0; i < address; i ++) {
    			if((hexArr[i] > 32) && (hexArr[i] < 127) || (hexArr[i] > 160)) {
    				hexStr += hexArr + "  ";
    			} else {
    				hexStr += "\n";
    			}
    		}
    		return hexStr;
     
    		/**char[] hexChar = "0123456789ABCDEF".toCharArray();
    		address = 
    				8
    				+ 3;
    		int firstHex = address;
    		int fd = (address - 8)/4;
     
    		for(int i = fd; i < address; i++) {
    			if((hexArr[i] > 32) && (hexArr[i] < 127) || (hexArr[i] > 160)) {
    				hexStr += hexArr[i];
    			} else {
    				hexStr += ".";
    			}
    		}
    		int i = 0;
    		String hexStr = "";
    		StringBuffer sb = new StringBuffer(hexStr);
    		do {
    			for(int j = 0; j < hexArr.length; j++) {
     
    				i += address;
    			}
    		} while (i < hexArr.length);
    		int[] hexArr2 = new int[i];
    		return sb.toString();*/
    	}
     
    	public String hex2Str(int convert, int numDigit) {
    		StringBuilder strBldr = new StringBuilder(Integer.toString(convert, 16));
    		if(strBldr.length() < numDigit) {
    			int hex = (numDigit - strBldr.length());
    			for(int i = 0; i <= hex; i++) {
    				strBldr.insert(0, "0");
    			}
    		} else if(strBldr.length() > numDigit) {
    			strBldr = new StringBuilder();
    			for(int i = 1; i <= numDigit; i++) {
    				strBldr.append(".");
    			}
    		}
    		/**
    		//String str = String.format("%08x", Integer.valueOf(numDigit));
    		//String hexStr = "";
    		int num = 0;
    		for(int i = convert; i < convert + numDigit; i++) {
    			num = (convert >> 4 * (numDigit - i - 1)) & 0x0f;
    			//str = Integer.toString(num + 0x100, 16).substring(i);
    		}
    		return (HEXARR[num]);*/
    		return strBldr.toString();
     
     
     
    	}
    }

    I just compiled/ran this one and get the nullnullnull output. I realize that I am getting a null pointer, but I am supposed to be getting something like this:
    00000000 50 4b 03 04 14 00 08 00  08 00 37 62 4c 3c 00 08 PK........7b<..

    So I guess I am having issues with my formatDump method, it appears that I need to somehow call the method in the actionPerformed for the Dump button and I also need to call the hex2Str method in the same actionPerformed. The only problem, I don't think that I have these methods set up correctly. Can you please also look at these methods and let me know if I am on the right track. Thank you

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dumping a file

    I get many compiler errors because of missing import statements.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dumping a file

    sorry here is the entire code:
    import java.awt.Color;
    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.Font;
     
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    import javax.swing.SwingConstants;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.PrintWriter;
     
     
    public class Week03 {
     
    	private JFrame frame;
    	private final JLabel lblNewLabel = new JLabel("@ ! @ Hex Dump @ ! @");
    	private final JLabel lblInstructions = new JLabel("Hex Dump program that " +
    			"will read a file and save the data that has been converted to " +
    			"hexadecimal form in a new file.");
    	private final JLabel lblFind = new JLabel("Find the following file:");
    	private final JTextField txtFind = new JTextField();
    	private final JButton btnFind = new JButton("Find");
    	private final JLabel lblSave = new JLabel("Save the following file:");
    	private final JTextField txtSave = new JTextField();
    	private final JButton btnSave = new JButton("Save");
    	private final JButton btnDump = new JButton("DUMP");
    	private static final int[] HEXARR = null;;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Week03 window = new Week03();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the application.
    	 */
    	public Week03() {
    		initialize();
    	}
     
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		txtFind.setFont(new Font("Andy", Font.PLAIN, 14));
    		txtFind.setBounds(10, 107, 589, 32);
    		txtFind.setColumns(10);
    		frame = new JFrame();
    		frame.setTitle("Belinda Frederick - Week03");
    		frame.setBackground(new Color(70, 130, 180));
    		frame.setBounds(100, 100, 715, 303);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
    		lblNewLabel.setForeground(new Color(46, 139, 87));
    		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
    		lblNewLabel.setFont(new Font("Fine Hand", Font.BOLD, 30));
    		lblNewLabel.setBounds(10, 11, 679, 50);
     
    		frame.getContentPane().add(lblNewLabel);
    		lblInstructions.setForeground(new Color(139, 0, 0));
    		lblInstructions.setHorizontalAlignment(SwingConstants.CENTER);
    		lblInstructions.setFont(new Font("Corbel", Font.BOLD, 13));
    		lblInstructions.setBounds(10, 60, 679, 25);
     
    		frame.getContentPane().add(lblInstructions);
    		lblFind.setForeground(new Color(46, 139, 87));
    		lblFind.setFont(new Font("Harrington", Font.BOLD, 14));
    		lblFind.setBounds(10, 82, 227, 25);
     
    		frame.getContentPane().add(lblFind);
     
    		frame.getContentPane().add(txtFind);
    		btnFind.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				JFileChooser jfc = new JFileChooser();
    				int inFile = jfc.showOpenDialog(null);
    				if(inFile == JFileChooser.APPROVE_OPTION) {
    					txtFind.setText(
    							jfc.getSelectedFile().getAbsolutePath());
    				}
    				txtSave.setText(txtFind.getText() + ".dmp");
    			}
    		});
    		btnFind.setForeground(new Color(248, 248, 255));
    		btnFind.setBackground(new Color(139, 0, 0));
    		btnFind.setFont(new Font("Batang", Font.BOLD, 14));
    		btnFind.setBounds(610, 109, 79, 30);
     
    		frame.getContentPane().add(btnFind);
    		lblSave.setForeground(new Color(46, 139, 87));
    		lblSave.setFont(new Font("Harrington", Font.BOLD, 14));
    		lblSave.setBounds(10, 151, 227, 25);
     
    		frame.getContentPane().add(lblSave);
    		txtSave.setFont(new Font("Andy", Font.PLAIN, 14));
    		txtSave.setColumns(10);
    		txtSave.setBounds(10, 175, 589, 32);
     
    		frame.getContentPane().add(txtSave);
    		btnSave.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JFileChooser jfc = new JFileChooser();
    				int inFile = jfc.showSaveDialog(null);
    				if(inFile == JFileChooser.APPROVE_OPTION) {
    					txtSave.setText(
    							jfc.getSelectedFile().getAbsolutePath());
    				}
    			}
    		});
    		btnSave.setForeground(new Color(248, 248, 255));
    		btnSave.setFont(new Font("Batang", Font.BOLD, 14));
    		btnSave.setBackground(new Color(139, 0, 0));
    		btnSave.setBounds(610, 177, 79, 30);
     
    		frame.getContentPane().add(btnSave);
    		btnDump.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String inStr = txtFind.getText();
    				String outStr = txtSave.getText();
    				//String hexData = null;
    				FileInputStream fis = null;
    				PrintWriter pw = null;
    				try {
    					fis = new FileInputStream(inStr);
    					pw = new PrintWriter(outStr);
     
    					int k;
    					byte[] buffer = new byte[16];
    					while((k = fis.read(buffer)) != -1) {
    						for(int i = 0; i < k; i++) {
    							int j = buffer[i];
    							if(j < 0) j += 256;
    							pw.print(HEXARR);
    						}
    						pw.printf("\n");
    					}
    				} catch (Exception fe) {
     
    				} finally {
    					if(fis != null) {
    						try {
    							fis.close();
    						} catch (IOException e_a) {
    							e_a.printStackTrace();
    						}
    					} if(pw != null) {
    						pw.close();
    					}
    				}
    				JOptionPane.showMessageDialog(null, "The hex dump is " +
    						"complete.");
    			}
    		});
    		btnDump.setForeground(new Color(248, 248, 255));
    		btnDump.setBackground(new Color(46, 139, 87));
    		btnDump.setFont(new Font("Fine Hand", Font.BOLD, 18));
    		btnDump.setBounds(280, 222, 133, 32);
     
    		frame.getContentPane().add(btnDump);
    	}
     
    	public static String formatDump(int address, int[] hexArr) {
    		String hexStr = "";
    		address = hexArr.length;
    		for(int i = 0; i < address; i ++) {
    			if((hexArr[i] > 32) && (hexArr[i] < 127) || (hexArr[i] > 160)) {
    				hexStr += hexArr + "  ";
    			} else {
    				hexStr += "\n";
    			}
    		}
    		return hexStr;
     
    		/**char[] hexChar = "0123456789ABCDEF".toCharArray();
    		address = 
    				8
    				+ 3;
    		int firstHex = address;
    		int fd = (address - 8)/4;
     
    		for(int i = fd; i < address; i++) {
    			if((hexArr[i] > 32) && (hexArr[i] < 127) || (hexArr[i] > 160)) {
    				hexStr += hexArr[i];
    			} else {
    				hexStr += ".";
    			}
    		}
    		int i = 0;
    		String hexStr = "";
    		StringBuffer sb = new StringBuffer(hexStr);
    		do {
    			for(int j = 0; j < hexArr.length; j++) {
     
    				i += address;
    			}
    		} while (i < hexArr.length);
    		int[] hexArr2 = new int[i];
    		return sb.toString();*/
    	}
     
    	public String hex2Str(int convert, int numDigit) {
    		StringBuilder strBldr = new StringBuilder(Integer.toString(convert, 16));
    		if(strBldr.length() < numDigit) {
    			int hex = (numDigit - strBldr.length());
    			for(int i = 0; i <= hex; i++) {
    				strBldr.insert(0, "0");
    			}
    		} else if(strBldr.length() > numDigit) {
    			strBldr = new StringBuilder();
    			for(int i = 1; i <= numDigit; i++) {
    				strBldr.append(".");
    			}
    		}
    		/**
    		//String str = String.format("%08x", Integer.valueOf(numDigit));
    		//String hexStr = "";
    		int num = 0;
    		for(int i = convert; i < convert + numDigit; i++) {
    			num = (convert >> 4 * (numDigit - i - 1)) & 0x0f;
    			//str = Integer.toString(num + 0x100, 16).substring(i);
    		}
    		return (HEXARR[num]);*/
    		return strBldr.toString();
     
     
     
    	}
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Dumping a file

    Have you looked at the variable that you are printing to the file?

    For testing change the program so everything is in the program with no need to read or write disk files. Use ByteArray streams so you can print them from the program and see the results immediately in the program.
    Last edited by Norm; April 28th, 2012 at 02:03 PM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: February 14th, 2012, 03:28 PM
  2. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  3. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  4. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM