我刚开始学习Java,我正在研究Java中的音频隐写项目(即将文本文件隐藏在wav文件中)。
为此,我将文本和wav文件存储在两个文件中。现在我想用字节读取它们并用文本文件位替换wav文件的随机位。
我如何以字节读取文件? (我尝试过使用InputStream)。是否有可能用文本文件的位替换wav文件中的随机位?它有什么功能吗? 一旦我读完文件(文本和wav文件),我如何编辑(和替换)wav文件的字节?
这是第一个屏幕,当用户点击HIDE按钮时,下一个屏幕会打开。
public class OptionScreen extends javax.swing.JFrame {
/** Creates new form OptionScreen */
public OptionScreen() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
hide = new javax.swing.JButton();
unhide = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
hide.setText("Hide");
hide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
hideActionPerformed(evt);
}
});
unhide.setText("UnHide");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(hide, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(94, 94, 94)
.addComponent(unhide, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(81, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(109, 109, 109)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(hide, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(unhide, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(101, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void hideActionPerformed(java.awt.event.ActionEvent evt) {
second s=new second();
s.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new OptionScreen().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton hide;
private javax.swing.JButton unhide;
// End of variables declaration
}
import java.io.DataInputStream;
import java.io.File;
import java.io.File.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
public class second extends javax.swing.JFrame {
private File afile; // put at bottom of class
private File tfile;
/** Creates new form second */
public second() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
text = new javax.swing.JButton();
audio = new javax.swing.JButton();
tname = new javax.swing.JLabel();
aname = new javax.swing.JLabel();
begin = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
text.setText("TEXT FILE");
text.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textActionPerformed(evt);
}
});
audio.setText("AUDIO FILE");
audio.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
audioActionPerformed(evt);
}
});
begin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
begin.setText("BEGIN");
begin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
beginActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(87, 87, 87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(text, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(audio, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(tname, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(aname, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addGap(270, 270, 270)
.addComponent(begin, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(31, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(text)
.addComponent(tname, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(audio)
.addComponent(aname, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(begin, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void textActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser opentext=new JFileChooser();
opentext.showOpenDialog(this);
int returnVal = opentext.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
tfile = opentext.getSelectedFile();
String tfname=tfile.getName();
tname.setText(tfname);
// ... code that loads the contents of the file in the text area
}
}
private void audioActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser openaudio=new JFileChooser();
openaudio.showOpenDialog(this);
int returnVal = openaudio.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
afile = openaudio.getSelectedFile();
aname.setText(afile.getName());
// ... code that loads the contents of the file in the text area
}
}
private void beginActionPerformed(java.awt.event.ActionEvent evt) {
try {
InputStream fint = new FileInputStream(tfile);
byte []bt=new byte[(int)afile.length()];
InputStream fina = new FileInputStream(afile);
int offset = 0;
int numRead = 0;
//
try {
System.out.println(new DataInputStream(fint).readByte());
} catch (IOException ex) {
Logger.getLogger(second.class.getName()).log(Level.SEVERE, null, ex);
}
//
System.out.println(bt);
//////////////////////////////////////////////////////////////////
FileOutputStream outs=new FileOutputStream(new File("C:\\Documents and Settings\\Administrator\\My Documents\\My Music\\Secret.wav"));
} catch (FileNotFoundException ex) {
}
//end of try catch
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new second().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel aname;
private javax.swing.JButton audio;
private javax.swing.JButton begin;
private javax.swing.JButton text;
private javax.swing.JLabel tname;
// End of variables declaration
}
答案 0 :(得分:1)
您有两种方法:(1)将文件读入字节数组,而不是在内存中操作,然后将其写回磁盘。 (2)使用RandomAccessFile,它允许你直接在磁盘上操作文件。