Best Projects

Bug Trackingsystem,Books online shopping,college management system,HR management system,Internet banking,Online chat room ,Online exam,Telephone billing system,Banking application,ATM database,airways reservation system,virtual network computing ,calculator,SMTP(simple mail transfer protocol)

File to Array Convertor (JAR file)

Description: It is very nice to build programs without dependence on picture or sound external files!!!
Here is the solution! This converter converts a chosen file to Java byte array
that may be used by create image method


import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class JConverter extends JFrame{
private Container container = getContentPane();
private JProgressBar progressBar = new JProgressBar();
private JFileChooser chooser = chooser = new JFileChooser();
private JMenuBar bar = new JMenuBar();
private JMenu menuFile = new JMenu("File");
private JMenuItem menuConvertFile = new JMenuItem("Convert File");
private JMenuItem menuExit = new JMenuItem("Exit");
private JMenu menuHelp = new JMenu("Help");
private JMenuItem menuSite = new JMenuItem("Visit web site");
private JMenuItem menuAbout = new JMenuItem("About");
private JPanel panel = new JPanel();
private JLabel labelNameOfArray = new JLabel("Array name");
private JTextField nameOfArray = new JTextField(10);
private JLabel labelBytesPerLine = new JLabel("How many bytes per line");
private JTextField bytesPerLine = new JTextField(3);

private class PerformFile extends Thread{
public void run(){
File theOpenedFile=null;
FileInputStream in;
FileOutputStream out;
int resultOfChoice;
int readFromFileResult;
int maxColomnCount;
byte fileReadArray[];
int openedFileLength=0;

chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
resultOfChoice = chooser.showOpenDialog(null);
theOpenedFile = chooser.getSelectedFile();
if (resultOfChoice==chooser.CANCEL_OPTION) return;
if (!(theOpenedFile==null||theOpenedFile.getName().equals(""))){
try{
in = new FileInputStream(theOpenedFile.getPath());
menuConvertFile.setEnabled(false);
out = new FileOutputStream(theOpenedFile.getPath() + ".j");
openedFileLength = (int)theOpenedFile.length();
progressBar.setMaximum(openedFileLength);
out.write(' '); out.write(' ');
out.write('b'); out.write('y'); out.write('t'); out.write('e'); out.write(' ');
for (int i=0;i out.write('['); out.write(']'); out.write('='); out.write('{');
out.write(13); out.write(10); out.write(' '); out.write(' '); out.write(' '); out.write(' ');
fileReadArray = new byte[openedFileLength];
readFromFileResult = in.read(fileReadArray);

try{
maxColomnCount = Integer.parseInt(bytesPerLine.getText());
if (maxColomnCount<10) maxColomnCount=10;
if (maxColomnCount>1000) maxColomnCount=1000;
}
catch(NumberFormatException e){maxColomnCount=20;}
bytesPerLine.setText(""+maxColomnCount);

for (int i=0;i if (i>0) out.write(',');
if (i % maxColomnCount == (maxColomnCount-1)){
out.write(13); out.write(10); out.write(' '); out.write(' '); out.write(' '); out.write(' ');
}
try{
out.write(Byte.toString(fileReadArray[i]).charAt(0));
out.write(Byte.toString(fileReadArray[i]).charAt(1));
out.write(Byte.toString(fileReadArray[i]).charAt(2));
out.write(Byte.toString(fileReadArray[i]).charAt(3));
} catch(StringIndexOutOfBoundsException e){}
progressBar.setValue(i+1);
progressBar.setString(""+((i+1)*100/openedFileLength)+"%");
progressBar.setStringPainted(true);
}

out.write(13); out.write(10); out.write(' '); out.write(' ');
out.write('}'); out.write(';');
out.flush();
JOptionPane.showMessageDialog(null,"The file "+theOpenedFile.getPath()+" successfully converted.","Conversion Completted",JOptionPane.INFORMATION_MESSAGE);
in.close();
out.close();
}
catch(IOException e){
System.out.println("Wrong Input or Output");
}
}
menuConvertFile.setEnabled(true);
}
}

public JConverter(){
super("Converter of files into the Java array format (Example: byte newArray[]={...})");
container.add(progressBar,BorderLayout.CENTER);
container.add(panel,BorderLayout.SOUTH);
panel.add(labelNameOfArray);
panel.add(nameOfArray);
panel.add(labelBytesPerLine);
panel.add(bytesPerLine);
nameOfArray.setText("newArray");
bytesPerLine.setText("20");
progressBar.setPreferredSize(new Dimension(600,30));
menuConvertFile.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
PerformFile performFile = new PerformFile();
performFile.start();
}
}
);
menuExit.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
);
menuSite.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane jOptionPane = new JOptionPane();
JTextField jTextField = new JTextField();
jTextField.setText("http://softcollection.sytes.net/javaprog");
jOptionPane.showMessageDialog(null,
jTextField,
"Please visit web site!",
JOptionPane.INFORMATION_MESSAGE);
}
}
);
menuAbout.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"Converter of files into the Java array format v1.08","About",JOptionPane.INFORMATION_MESSAGE);
}
}
);
bar.add(menuFile);
menuFile.setMnemonic('F');
menuFile.add(menuConvertFile);
menuFile.add(menuExit);
bar.add(menuHelp);
menuHelp.setMnemonic('H');
menuHelp.add(menuSite);
menuHelp.add(menuAbout);
setJMenuBar(bar);
pack();
setResizable(false);
show();
}

public static void main(String args[]){
JConverter jConverter=new JConverter();
jConverter.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}






No comments: