gpt4 book ai didi

java - 如何读取 wav 文件并替换其中的位?

转载 作者:太空宇宙 更新时间:2023-11-04 08:32:46 25 4
gpt4 key购买 nike

我刚刚开始学习 Java,正在开发一个 Java 音频隐写项目(即将文本文件隐藏在 wav 文件中)。

为此,我将文本和 wav 文件存储在两个文件中。现在我想以字节为单位读取它们,并用文本文件位替换 wav 文件的随机位。

如何以字节为单位读取文件? (我尝试过使用InputStream)。是否可以用文本文件的位替换 wav 文件中的随机位?有什么功能可以实现吗?读取文件(文本和 wav 文件)后,如何编辑(和替换)wav 文件的字节?

这是第一个屏幕,当用户点击“隐藏”按钮时,会打开下一个屏幕。

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
}

最佳答案

您有两种方法:(1) 将文件读入字节数组,然后在内存中对其进行操作,然后将其写回磁盘。 (2) 使用 RandomAccessFile 允许您直接操作磁盘上的文件。

关于java - 如何读取 wav 文件并替换其中的位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7146382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com