Si e' vero ma rimane sempre il fatto che nel main() devo scrivere le stringhe su file,
cosi come l'hai progettato tu il metodo e' perfettamente funzionante ma mi restituisce
void.
Di seguito c'e' il main() che scrive i dati su file e puoi vedere come il metodo della classe
che ho postato restituisce l'array di permutazioni.
codice:
public static void main(String[] args) {
String[] STRpermutazioni;
STRpermutazioni = PermutaPhrase.permuta("il mio gatto");
File aFile = new File("C:/Beg Java Stuff/permutation.txt");
OutChannel channel = new OutChannel(aFile, true, false);
ByteBuffer byteBufer = ByteBuffer.allocateDirect(STRpermutazioni[0].length() + 1);
for (int i = 0; i < STRpermutazioni.length; i++) {
byteBufer.put((STRpermutazioni[i] + "\n").getBytes());
byteBufer.flip();
channel.write(byteBufer);
byteBufer.clear();
}
System.out.println("File written is " + channel.size() + " bytes.");
channel.close();
}
La classe OutChannel l'ho scritta per ridurre il codice attinente alla scrittura di dati
sul file, quindi non ci fare caso ma te la posto lo stesso:
codice:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import javax.swing.JFileChooser;
/**
*
* @author mau2
*/
/*in questa classe bisogna includere altri costruttori
*che prevedano l'apertura dello stream con gli altri costruttori
*overloaded della classe FileOutputStream*/
public class OutChannel{
private FileOutputStream outStream;
private FileChannel outChannel;
private FileLock lock = null;
private long position;
private boolean random = false;
/** Creates a new instance of ChannelUtility */
public OutChannel(File file, boolean createDirectory, boolean append){
if(createDirectory && !file.exists())
createDir(file);
try {
outStream = new FileOutputStream(file, append);
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
}
outChannel = outStream.getChannel();
}
public int write(ByteBuffer buf){
int tmp = buf.limit() - buf.position();
try{
if(random){
outChannel.write(buf, position);
position += tmp;
}else
outChannel.write(buf);
outChannel.force(true);
} catch (IOException e) {
e.printStackTrace(System.err);
}
return tmp;
}
public int write(ByteBuffer buf, long position){
int size = -1;
try {
size = outChannel.write(buf, position);
outChannel.force(true);
} catch (IOException e) {
e.printStackTrace(System.err);
}
return size;
}
public long write(ByteBuffer[] bufs){
long size = -1;
long tmp = 0;
ByteBuffer tmpBuf = null;
for (int i = 0; i < bufs.length; i++) {
tmp += bufs[i].limit() - bufs[i].position();
}
tmpBuf = ByteBuffer.allocate((int)tmp);
for (int i = 0; i < tmp; i++) {
tmpBuf.put(bufs[i]);
}
tmpBuf.flip();
try {
if(random){
size = outChannel.write(tmpBuf, position);
position += tmp;
outChannel.force(true);
}else
size = outChannel.write(tmpBuf);
outChannel.force(true);
} catch (IOException e) {
e.printStackTrace(System.err);
}
return size;
}
public long write(ByteBuffer[] bufs, int offset, int length ){
long size = -1;
long tmp = 0;
ByteBuffer tmpBuf = null;
for (int i = offset; i < offset + length; i++) {
tmp += bufs[i].limit() - bufs[i].position();
}
tmpBuf = ByteBuffer.allocate((int)tmp);
for (int i = offset; i < offset + length; i++) {
tmpBuf.put(bufs[i]);
}
tmpBuf.flip();
try {
if(random){
size = outChannel.write(tmpBuf, position);
position += tmp;
}else
size = outChannel.write(tmpBuf);
outChannel.force(true);
} catch (IOException e) {
e.printStackTrace(System.err);
}
return size;
}
public OutChannel position(long newPosition){
position = newPosition;
random = true;
return this;
}
public long position(){
long seek = -1;
if(random){
seek = position;
} else{
try {
seek = outChannel.position();
} catch (IOException e) {
}
}
return seek;
}
public void append(){
random = false;
position = 0;
}
public boolean isRandom(){
return random;
}
public void close(){
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
public void force(){
try {
outChannel.force(true);
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
public long size() {
long tmp = 0;
try {
tmp = outChannel.size();
} catch (IOException e) {
}
return tmp;
}
public MappedByteBuffer map(FileChannel.MapMode mode, long position, long size){
MappedByteBuffer buf = null;
try {
buf = outChannel.map(mode, position, size);
} catch (IOException e) {
e.printStackTrace(System.err);
}
return buf;
}
//questo metodo non l'ho completato perche' gia' presente sulla classe InChannel
//in quanto piu' naturale nel codice
private long transferFrom(ReadableByteChannel src, long position, long count) throws IOException {
return -1;
}
public FileLock tryLock(long position, long size, boolean shared){
try {
lock = outChannel.tryLock(position, size, shared);
} catch (IOException e) {
e.printStackTrace(System.err);
}
return lock;
}
public FileLock tryLock(long position, long size){
try {
lock = outChannel.tryLock(position, size, false);
} catch (IOException e) {
e.printStackTrace(System.err);
}
return lock;
}
public void tryLock(){
try {
lock = outChannel.tryLock();
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
public boolean isLocked(){
if(lock == null)
return false;
else
return true;
}
public void releaseLock(){
try {
lock.release();
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
public FileChannel getOriginalChannel() {
return outChannel;
}
public boolean isOpen() {
return outChannel.isOpen();
}
/**
*
* @param file
*/
private void createDir(File file){
File dir = new File(file.getParent());
if (!dir.exists()) // If directory does not exist
{
if (!dir.mkdir()) // ...create it
{
System.out.println("Cannot create directory: " + file);
System.exit(1);
}
} else if (!dir.isDirectory()) {
System.err.println(file + " is not a directory");
System.exit(1);
}
}
public FileChannel truncate(long size) throws IOException {
return null;
}
protected void implCloseChannel() throws IOException {
}
}